Created
February 20, 2011 02:45
-
-
Save daveWid/835633 to your computer and use it in GitHub Desktop.
A simple function to parse a Twitter post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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( | |
'<a href="${0}">${0}</a>', | |
'<a href="http://twitter.com/${0}">${0}</a>', | |
'<a href="http://search.twitter.com/search?q=${0}">${0}</a>', | |
); | |
$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