Does not work, the $1-value is lost when calling the function:
echo preg_replace('"\b(http://\S+)"', '<a href="$1">'.findTopDomain('$1').'</a>', $text);
Works fine, outputs: stackoverflow.com
echo preg_replace('"\b(http://\S+)"', '<a href="$1">'.findTopDomain('http://stackoverflow.com/questions/ask').'</a>' , $text);
I need to send the $1 value to a function from within preg_replace.
What am I doing wrong?
You need to set the e modifier to have the substitution expression to be executed:
Note that your substitution now has to be a valid PHP expression. In this case the expression would be evaluated to:
And don’t forget to escape the output with at least
htmlspecialchars: