Jump to content

Small Regex help


HubertCumperdale

Recommended Posts

I have to admit that regex is something that always sends me crosseyed.... one day I will get my head around it.

 

I have a small piece of code that is used to create hyperlinks for things like #Facebook from a Twitter RSS feed.

It works ok, however sometimes we get: #Facebook, or #Facebook: or #Facebook.

 

What I would like is to also remove these common end characters such a commas, colons and periods.

 

This is the code I currently have:

 

$description = preg_replace("/#([^ ]+)/", "<a href=\"https://twitter.com/#!/search/%23$1\">#$1</a>", $description);

 

Any help appreciated!

 

 

Link to comment
https://forums.phpfreaks.com/topic/261337-small-regex-help/
Share on other sites

This will throw those 3 chars into the mix, to match up to.  It says to match one or more of anything that is not a space, comma, pound or colon:

 

$description = preg_replace("/#([^ ,#:]+)/", "<a href=\"https://twitter.com/#!/search/%23$1\">#$1</a>", $description);

 

or alternatively, this will just match one or more word char (letter, number or underscore)

 

 

$description = preg_replace("/#(\w+)/", "<a href=\"https://twitter.com/#!/search/%23$1\">#$1</a>", $description);

 

Link to comment
https://forums.phpfreaks.com/topic/261337-small-regex-help/#findComment-1339200
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.