How to link Twitter usernames with PHP preg_replace?

I want to find the text property of my twitter status objects and change the @username name to <a href="http:/twitter.com/username">@username</a> . What I have tried so far is as follows:

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

But there are no replacements. I know that my reg exp is wrong, but I can’t pinpoint where / why. Help?

** Edit: ... sample data on request?

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

Required Conclusion:

@janesmith I like it, but my friend @johndoe said 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; } 
+4
source share
4 answers

Why does \ exist before _ ? Does it work if you choose \ ? Although this should not have disrupted the functionality ...

It may be useful to change \1 to \\1 so that you are sure that the backslash is escaped; or better (starting with PHP 4.0.4) $1 . But then again, it had to work as it is, in single quotes.

In addition, you can simplify:

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

I have slightly updated the hashtag support 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, "\ 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; }

+2
source

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

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

0
source

Twitter has updated their api and, at least for me, it’s a more convenient user interface. Check the documentation found at dev.twitter.com/docs/twitter-for-websites click here . First you have to create an application, follow the step by step instructions, then create a widget. The widget that you see below displays my tweets, but you can create one that displays all the actions on your timeline or search results, 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> 
0
source

All Articles