On my website, I have comments.
In my comment string, I find all username mentions like so (username mentions start with /u/, for example /u/felix
):
preg_match_all('#/u/([a-z0-9]+)#i', $comment, $matches);
Now I have an array of usernames called $matches
.
I want to then replace all username matches in $comment
with something like this:
<a href="/u/felix">/u/felix</a>
I tried doing a foreach solution with a str_replace, however I run into the problem of having users that contain the usernames of other users. So if we had the users "fel", feli" and "felix, the loop would do it 3 times for "fel".
How can I do this?