So I have this nice little function that converts hastags to clickable links (URLs), anyone is free to use this if they please btw.
<?php
if (!function_exists('convertHashtags')) {
function convertHashtags($str) {
$regex = "/#+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '<a href="hashtag?tag=$1">$0</a>', $str);
return($str);
}
}
$convert_hashtag = convertHashtags("#Sitepoint is the best forum");
echo $convert_hashtag;
?>
When I modify this function to have the @ sign instead of the # sign it converts @username to a clickable link.
<?php
if (!function_exists('convertMention')) {
function convertMention($str) {
$regex = "/@+([a-zA-Z0-9_]+)/";
$str = preg_replace($regex, '<a href="profile?username=$1">$0</a>', $str);
return($str);
}
}
$convert_mention = convertMention("@Sitepoint is the best forum");
echo $convert_mention;
?>
Is there any way I can merge the two so that I can turn both the #hashtags and the @mentions into clickable links? I’m not a “pro” PHP programmer but if anyone can help that’d be great .
The advantage of the former approach is that you can still use the individual converters in isolation – it’s generally better to have a function do exactly one thing. The disadvantage however is that you’d also parse some already replaced parts. This should be no problem in this case, but if the links would themselves contain hashes (for example), it would result in garbage; so the latter approach is probably cleaner.
This was a great help but I found a little flaw in this code. I am using this code for a comment system, and the people who are using the @mentionusername feature said that it does not allow the following: @mention.username.
Notice the subtle " . "
It’ll allow everything else but it won’t allow something like @mention.username. It does this @mention.username. It links the @mention but not the .username part.
A period is one of the characters that has a special meaning in regex, so it must be escaped with the backslash to say you are using that character as an actual character.
What I’m trying to do: pull all posts that have the same hashtags.
So ex: If there are 20 posts in my database and out of the 20, 10 of them have the same hashtag (#firstpost), I’d like to get all 10 posts that have the hashtag (#firstpost) and display them on the page.