rhodesjason
rhodesjason

Reputation: 5014

How do I linkify Twitter usernames using PHP preg_replace?

I want to search the text property of my twitter status objects and swap out @username for <a href="http:/twitter.com/username">@username</a>. What I have tried so far looks like this:

$pattern = '/([@]{1})([a-zA-Z0-9\_]+)/';
$replace = '<a href="http://twitter.com/\2">\1\2</a>';
$new_string = preg_replace($pattern, $replace, $text);

But it doesn't do any replacements. I know my reg exp is wrong but I can't figure out exactly where/why. Help?

**Edit: ... sample data as requested?

$text = '@janesmith I like that, but my friend @johndoe said it better.';

Desired output:

@janesmith I like that, but my friend @johndoe said it better.

***** MY FULL FUNCTION *****

function linkify($string, $twitter=false) {

    // reg exp pattern
    $pattern = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

    // convert string URLs to active links
    $new_string = preg_replace($pattern, "<a href=\"\\0\">\\0</a>", $string);

    if ($twitter) {
        $pattern = '/@([a-zA-Z0-9_]+)/';
        $replace = '<a href="http://twitter.com/\1">@\1</a>';
        $new_string = preg_replace($pattern, $replace, $new_string);
    }

    return $new_string;
}

Upvotes: 4

Views: 3096

Answers (4)

Steve Kinzey
Steve Kinzey

Reputation: 371

Twitter has updated their api and, at least to me, it is way more user friendly. Check out the documentation found at the dev.twitter.com/docs/twitter-for-websites click here. You must first creat an app, follow the step-by-step instructions, then you create a widget. The widget you see below displays my tweets, but you can create one that displays all the action on your timeline or the results of searches, lists, etc...

     <a class="twitter-timeline" ref="https://twitter.com/SKAmerica" data-widget-id="359949405157203970">Tweets by @SKAmerica</a>
     <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id))js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

Upvotes: 0

Refiner
Refiner

Reputation: 129

Found this to work for URLs, @usernames, and #hashtags:

http://davidwalsh.name/linkify-twitter-feed

Upvotes: 0

Akas84
Akas84

Reputation: 21

I have update a little the function to support hashtags too:

function linkify($string, $twitter=false) {

// reg exp pattern

$pattern = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(\/\S*)?/";

// convert string URLs to active links $new_string = preg_replace($pattern, "\0", $string);

if ($twitter) {

   $pattern = '/\@([a-zA-Z0-9_]+)/';

   $replace = '<a href="http://twitter.com/\1">@\1</a>';

   $new_string = preg_replace($pattern, $replace, $new_string);


   $pattern = '/\#([a-zA-Z0-9_]+)/';

   $replace = '<a href="http://twitter.com/search/#\1">#\1</a>';

   $new_string = preg_replace($pattern, $replace, $new_string);

}

return $new_string; }

Upvotes: 2

LarsH
LarsH

Reputation: 27996

Why is the \ there before _? Does it work if you take out the \? Though that shouldn't have broken the functionality...

It might be helpful to change the \1 to \\1 so that you're sure the backslash is escaped; or better (since PHP 4.0.4) $1. But again, it should have worked as-is, within single quotes.

Also, you can simplify:

$pattern = '/@([a-zA-Z0-9_]+)/';
$replace = '<a href="http://twitter.com/$1">@$1</a>';

Upvotes: 5

Related Questions