Question

I need some help improving this function I made for parsing the links in a Twitter. It creates links for hashtags and @replys. It all works fine, the problem is if a hashtag or @reply has a punctuation character at the end with no space, it gets added to the HREF URL.

For example if I Tweeted "I really like #pizza, and #pop", the link for the #pizza would be wrong since it will have a comma in it.

I'm not very good with Regex, this took me awhile just to get this working, so any help would be awesome!

function parse_tweet($description, $colour='', $external=false) {
if ($external == true) $pre = 'http://politwitter.ca'; else $pre = '';
$description = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\" target=\"_blank\">$1</a>", $description);
$description = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>'", $description);
$description = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"".$pre."/user/\\2\" rel=\"nofollow\">@\\2</a>'", $description);
$description = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"".$pre."/hash/\\2\" rel=\"nofollow\" class=\"hash ".$colour."\">#\\2</a>'", $description);
return $description;

}

Was it helpful?

Solution

Simplest modification of your code to do what you want:

$description = preg_replace("#(^|[\n ])\#(\w+)#ise", "'\\1<a href=\"".$pre."/hash/\\2\" rel=\"nofollow\" class=\"hash ".$colour."\">#\\2</a>'", $description);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top