How to use a static method as the callback parameter in preg_replace_callback()?

Viewed 10503

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?

3 Answers

You can also use T-Regx library:

pattern($pattern)->replace($input)->callback([Utilities::class, 'LinksCallback']);
Related