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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.