preg_replace_callback() problem

In PHP when using a class method as a callback, you must use the array form of callback. That is, you create an array the first element of which is the class (if the method is static) or an instance of the class (if not), and the second element is the function to call. E.g.

class A {
     public function cb_regular() {}
     public static function cb_static() {}
}

$inst = new A;

preg_replace_callback(..., array($inst, 'cb_regular'), ...);

preg_replace_callback(..., array('A', 'cb_static'), ...);

The function you are calling of course has to been visible from within the scope where you are using the callback.

See http://php.net/manual/en/language.pseudo-types.php, for details of valid callbacks.

N.B. Reading there, it seems since 5.2.3, you can use your method, as long as the callback function is static.


You can do it like this:

return preg_replace_callback($pattern, array("Utilities", "LinksCallback"), $input)

Reference: http://php.net/callback

Tags:

Php

Regex