Skip to content

Instantly share code, notes, and snippets.

@daveWid
Created February 20, 2011 02:45
Show Gist options
  • Save daveWid/835633 to your computer and use it in GitHub Desktop.
Save daveWid/835633 to your computer and use it in GitHub Desktop.
A simple function to parse a Twitter post
<?php
/**
* Formats a tweet with links
*
* @author Dave Widmer
* @see http://www.davewidmer.net
*
* @param string Tweet
* @return string Formatted Tweet
*/
function format_tweet( $tweet )
{
$search = array(
'/http[s]?://S+/', // URLs
'/@[A-Za-z0-9-_]+/', // @names
'/#[A-Za-z0-9-_]+/', // #hashtag
);
$replace = array(
'&lt;a href="${0}"&gt;${0}&lt;/a&gt;',
'&lt;a href="http://twitter.com/${0}"&gt;${0}&lt;/a&gt;',
'&lt;a href="http://search.twitter.com/search?q=${0}"&gt;${0}&lt;/a&gt;',
);
$tweet = preg_replace($search, $replace, $tweet);
return str_replace( array('/@', '=#') , array('/', '=%23') , $tweet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment