Simple Regular Expressions Function for Twitter Search

Doing some work that involves Twitter's search api. When a call is made JSON returns text of each message, which is to be expected, but many posts have "@soandso" or "http://bit.ly/dDnSkQ" or something, and that kind of info can't just be left to sit there now can it? No it can not. However, Using PHP, by applying some very simple regular expression functions in a method, voila! Links GAlore. Using preg_replace you can quickly and easily replace each text version of '@johnmangino' with @johnmangino. The only "trick" here is to use the parenthesis to give you a reference that will allow you to make the user link without the '@'. Here is a quick function used within the loop to href the links. Import the function and run the text through it like so:
$text = $r->text;  $text = makeRich($text);
So here is the function. Very very simple, but I'm finding it helpful in a few places. Right, I'm sure this can be greatly improved upon, but I hope it helps someone. Ok. Bye.

Posted via web from John Mangino's posterous

 
This entry was posted in Development and tagged , , , . Bookmark the permalink.