I'm using preg_replace_callback to find and replace textual links with live links:
http://www.example.com
to
<a href='http://www.example.com'>www.example.com</a>
The callback function I'm providing the function with is within another class, so when I try:
return preg_replace_callback($pattern, "Utilities::LinksCallback", $input);
I get an error claiming the function doesn't exist. Any ideas?
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 (dead link), for details of valid callbacks.http://php.net/manual/en/language.pseudo-types.php
N.B. Reading there, it seems since 5.2.3, you can use your method, as long as the callback function is static.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With