HubertCumperdale Posted April 20, 2012 Share Posted April 20, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/261337-small-regex-help/ Share on other sites More sharing options...
.josh Posted April 20, 2012 Share Posted April 20, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/261337-small-regex-help/#findComment-1339200 Share on other sites More sharing options...
HubertCumperdale Posted April 21, 2012 Author Share Posted April 21, 2012 Used the first one, works great.... thanks! Quote Link to comment https://forums.phpfreaks.com/topic/261337-small-regex-help/#findComment-1339266 Share on other sites More sharing options...
HubertCumperdale Posted April 21, 2012 Author Share Posted April 21, 2012 Actually, the second one is better... never know what char may be at the end of a word: . : ; ? - * etc..... Quote Link to comment https://forums.phpfreaks.com/topic/261337-small-regex-help/#findComment-1339267 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.