Papermashup

Subscribe


Tweets


"RT @bethgordon: .@Tesco Please rethink archaic 'Approved by Mums'. Pretty sure it's not just mothers who make childcare decisions. https://…"

@ashleyford 4 days ago

"Is there ever a time that @DFS doesn't have a sale on?"

@ashleyford 1 week ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

PHP regular expression Twitter links

AshleyAshley

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 ])@([^ "tnr<]*)#ise", "'\1@\2'", $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]+[^ "nrt<]*)#ise", "'\1\2'", $ret);
$ret = preg_replace("#(^|[n ])((www|ftp).[^ "tnr<]*)#ise", "'\1\2'", $ret);

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 21
  • Shanaya
    Posted on

    Shanaya Shanaya

    Reply Author

    The post is really very nice and admire this blog.


  • veículos
    Posted on

    veículos veículos

    Reply Author

    Hey there! I know this is kinda off topic however ,
    I’d figured I’d ask. Would you be interested in trading
    links or maybe guest authoring a blog article or vice-versa?
    My blog covers a lot of the same subjects as yours and I feel we could greatly benefit from each other.
    If you’re interested feel free to send me an email. I look forward to hearing from you! Awesome blog by the way!


  • proud navy parents
    Posted on

    proud navy parents proud navy parents

    Reply Author

    Almost all packaged goodies and cereals that are marketed to kids today
    have this impact on them. As one facebook fan with the page Breastfeeding in Combat
    Boots puts it, ‘It’s ‘taboo’ because men – by extension society
    – have a hard time separating the sexual aspect of breasts from the utilitarian
    purpose. what ever you choose, you also might
    be an “It Girl”.

    my blog – proud navy parents


  • Analisa Emas
    Posted on

    Analisa Emas Analisa Emas

    Reply Author

    Hey there! I’ve been following your site for a long time now and finally got the bravery to go ahead and give you a shout out from Houston Texas! Just wanted to mention keep up the good work!


  • ebooks
    Posted on

    ebooks ebooks

    Reply Author

    It’s going to be ending of mine day, however before end I am reading this wonderful paragraph to improve my knowledge.


  • chad le clos
    Posted on

    chad le clos chad le clos

    Reply Author

    My brother suggested I might like this blog.
    He was totally right. This post truly made my
    day. You can not imagine just how much time I
    had spent for this info! Thanks!


  • simple and profit
    Posted on

    simple and profit simple and profit

    Reply Author

    These are genuinely impressive ideas in about
    blogging. You have touched some pleasant things here.
    Any way keep up wrinting.


  • ekonomi hari ini
    Posted on

    ekonomi hari ini ekonomi hari ini

    Reply Author

    Somebody essentially lend a hand to make severely posts I would state.

    This is the first time I frequented your website page and up to
    now? I amazed with the analysis you made to make this particular post incredible.
    Excellent activity!


  • ioby.org
    Posted on

    ioby.org ioby.org

    Reply Author

    Hi there, I log on to your blog daily. Your writing style is witty, keep
    doing what you’re doing!


  • travel
    Posted on

    travel travel

    Reply Author

    I was incredibly impressed from the depth of data you chose to share listed here.
    Thank you for taking the time to jot down this kind of an insightful and useful piece


  • personal logo design
    Posted on

    personal logo design personal logo design

    Reply Author

    Hi there! I just would like to give you a huge thumbs
    up for your great info you have right here on this post. I
    will be coming back to your web site for more soon.


  • muxcmux
    Posted on

    muxcmux muxcmux

    Reply Author

    lol, ur comments ate my anchor tags 😀 haha


  • muxcmux
    Posted on

    muxcmux muxcmux

    Reply Author

    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


  • André Lima
    Posted on

    André Lima André Lima

    Reply Author

    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?


  • Dominik Deobald
    Posted on

    Dominik Deobald Dominik Deobald

    Reply Author

    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
      Posted on

      Ashley Ashley

      Reply Author

      @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.


  • kik3
    Posted on

    kik3 kik3

    Reply Author

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

    Please help me!


  • Harry
    Posted on

    Harry Harry

    Reply Author

    Thanks, found this extremely useful.


  • Jamie Bicknell
    Posted on

    Jamie Bicknell Jamie Bicknell

    Reply Author

    Would this not be cleaner and faster:

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