I have this function, which color every @username contained in a string
//color @username
function hashtag_links($string,$id_session) {
preg_match_all('/@(\w+)/',$string,$matches);
foreach ($matches[1] as $match) {
$string = str_replace("@$match", "<span class=color>@$match</span>", "$string");
}
return $string;
}
Everything is good while the usernames are different (@cat,@pencil,@scubadiving), but in case the username starts with the same letters (@cat,@caterpiller,@cattering), the functions only colors the repeated letters in this case (@cat), what to do?
Use preg_replace instead:
//color @username
function hashtag_links($string,$id_session) {
return preg_replace('/@(\w+)/', '<span class=color>@$1</span>', $string);
}