I am implimenting a timeline system on my website where users can mention others users in their timeline using @username like twitter.
I want to convert @username to link and point it to their profile
My string :
$timeline="@fred-ii 's posts on @stackoverflow are intresting.";
I am using the following code to replace @username with url :
echo preg_replace("/@([^\s]+)/i","<a href='http://example.com/$1'>@$1</a>",$timeline);
it works, the problem is that it matchs spaces also
this string
"@fred-ii 's posts on@stackoverflow";
There is no space between on and @stackoverflow ,I want to exclude it,
so i updated my regex
/\s+@([^\s]+)/
it worked but it didnot match the first part of my string (username @fred-ii ) .I think regex engine is looking for space|s at the start of string.
What do I need to change in my pattern to match all @usernames ?
$timeline="@fred-ii 's posts on @stackoverflow are intresting.";