SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Create a link from plain text

    Hi,

    I am using a Twitter plugin which outputs tweets as plain text. I would like to take the usernames and create links to their profiles from the text.

    So I would like...

    Coffee's great @WickedCoffeeCo @StuGoulden. Just not when you're already in a mood.

    To become...

    Coffee's great @WickedCoffeeCo @StuGoulden. Just not when you're already in a mood.

    So I'd like the script to read the text (which will be in a div with an ID of "tweets") and grab each @username and create

    HTML Code:
    <a href="http://twitter.com/username">@username</a>
    Is this possible? jQuery is already running if that helps : )

    Many thanks

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This solution assumes that you're using at least jQuery 1.4 (also, here's a fiddle):

    Code:
    $('#tweet').html(function (i, html) {
        return html.replace(/@([a-z]+)/ig, '<a href="http://twitter.com/$1">@$1</a>');
    });
    Another huge problem (in my opinion) is that it relies on the innerHTML property, which can cause serious issues on mobile. But that's more an issue with jQuery itself than this particular script.
    Last edited by sdleihssirhc; Sep 28, 2011 at 14:19. Reason: clarified meaning
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect! Thanks so much!! (Love the Fiddle touch as well, inspired)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •