- preg_replace_callback: documentation ( source)
- str_replace: documentation ( source)
<?php
$text = 'say hello to @elise and @john';
function replace_at_symbol($matches){
$username = str_replace("@", "", $matches[0]);
return '<a href="/profile/'.$username.'">'.$matches[0].'</a>';}
$output = preg_replace_callback("/([@][a-zA-Z-0-9]+)/", "replace_at_symbol", $text);
echo $output;
?>