PHP regular expression Twitter links

PHP regular expression Twitter links

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on January 16, 2009

Here’s a quick piece of code to highlight replies to users in twitter for example @ashleyford this script uses regular expression to find and replace a based on two things. Firstly it checks for a space before the @ symbol and then checks for the space at the end of the users name, it then takes that string and replaces it with a link to the user twitter profile. Useful if you have a dynamic list of tweets that you want to linkify. All you need to do is change $ret to the variable the contains your string.


$ret = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $ret);

These two lines of code do a similar job with regards the structure however they look for the http:// and www.  to determine a link.


$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $ret);
More tutorials from Papermashup
Comments
10 discussions around PHP regular expression Twitter links
  1. muxcmux says:

    lol, ur comments ate my anchor tags :D haha

  2. muxcmux says:

    Easier solution for both trends (#) and users (@)
    preg_replace(‘/#(\w+)/’, ‘#\1‘, $text)
    Just swap the # with a @ if you want to link to users :)
    Cheers

  3. André Lima says:

    When the @user is near a dot it includes the dot in the link. Ex: This is a test of @CocaCola. (result: Sorry, that page doesn’t exist!) Can you fix that?

  4. Okay, it broke my submitted HTML code… Another try to comment… ;)

    This regex will destroy your HTML code if you have something like this:

    <a href=”http://www.example.com” title=”Bla @test blubb”>Don\’t break!</a>

    • Ashley says:

      @Dominik As the title of the tutorial explains. This Regular expression is for use with the Twitter API, It is not designed to be used with normal text with HTML in it. There is no reason for a user to type HTML code into Twitter as it will be automatically stripped out.

  5. kik3 says:

    I need one regular expression for twitter trends links like #xxxx

    Please help me!

  6. Harry says:

    Thanks, found this extremely useful.

  7. Pingback: PHP form validation | Papermashup.com

  8. Jamie Bicknell says:

    Would this not be cleaner and faster:

    preg_replace(“/@([A-Za-z0-9_]+) /”,”$0“,$twitter->text);

  9. Pingback: PHP regular expression Twitter links | PHP-Blog.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.