I am working on a mentions system and I have run across a problem, when I am looping through and changing the mentions to links, it will replace similar ones such as @tom and @tom123 with the same link to /tom/ instead of individual.
I have tried using regex and preg_replace however I am also checking to see if the username actually exists, so I am just wondering if there is a way I can prevent it from happening.
$tweet = "@wayne how are you? @wayne123 is cool but are you?";
preg_match_all('/(^|\s)@([a-z0-9_]+)/i', $tweet, $matches);
$i = 0;
foreach( $matches[2] as $value )
{
if ( $db_query )
{
$tweet = str_replace("@" . $value, "<a href=\"/user/$value\">@$value</a>!", $tweet);
}
}
echo $tweet; // outputs: hi <a href="/user/wayne">@wayne!</a> how are you? <a href="/user/wayne">@wayne!</a>123 is cool but are you?
Any help is greatly appreciated, I have tried with regex before but it continues along the line and updates the others before I am even able to check to see if they are a valid user