I have the following code:
$message = preg_replace($pattern, "$this->get_bitly_url('$0')", $message);
function get_bitly_url($url) {
// do stuff to get the url
return $newurl;
}
I would like to replace the matches with the return value of the function get_bitly_url() in the same class.
But I got the following error:
Notice: Undefined property: MyClass::$get_bitly_url in /path/to/MyClass.php on line 43
So I changed to preg_replace with:
$message = preg_replace($pattern, "self::get_bitly_url('$0')", $message);
This works, but I would like to know whether this is the right way to do it.
So… is it?
No, definitely not :). Use preg_replace_callback: http://php.net/manual/en/function.preg-replace-callback.php.