I have those mysql users
1 master
2 mastercard
3 mastercom
In the next string , I want to link them to his own profile the users (@user)
$string=" Hi, I would like to met up with @master @mastercard @mastercom and @masterhigh ";
where @masterhigh doesnt belong to the mysql table and need no to be linked to his profile.
I have this code
preg_match_all('/@(\w+)/',$string,$matches);
foreach ($matches[1] as $match) {
//
$user=mysql_query("SELECT id_user FROM $table_users WHERE username='$match' LIMIT 1");
$user_n=mysql_fetch_array($user);
//num
$user_num=mysql_num_rows($user);
$id_user=$user_n['id_user'];
if($user_num>0){
//user exists (link to profile)
$imatch="<a href='?s=$id_$user'>@".$match."</a>";
}else{
//user NOT exists (NOT link to profile)
$imatch ="@$match";
}
$string=str_replace("@".$match,$imatch,$string);
}
echo $string;
While the users are differents, everything is Ok, but when they starts with the same letters, the code only links the repeated letters (@master) and not redirect to @mastercard profile or @mastercom profile. I think str_replace() is not working as expected. What I am doing wrong? 5 stars.