Forked from styledev/Parse Tweet Message to Link Entities
Created
November 21, 2013 15:26
-
-
Save matrixfox/7583541 to your computer and use it in GitHub Desktop.
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
function parse_message( &$tweet ) { | |
$replace_index = array(); | |
if ( !empty($tweet['entities']) ) { | |
foreach ($tweet['entities'] as $area => $items) { | |
switch ( $area ) { | |
case 'hashtags': | |
$find = 'text'; | |
$prefix = '#'; | |
$url = 'https://twitter.com/search/?src=hash&q=%23'; | |
break; | |
case 'user_mentions': | |
$find = 'screen_name'; | |
$prefix = '@'; | |
$url = 'https://twitter.com/'; | |
break; | |
case 'media': case 'urls': | |
$find = 'display_url'; | |
$prefix = ''; | |
$url = ''; | |
break; | |
default: break; | |
} | |
foreach ($items as $item) { | |
$text = $tweet['text']; | |
$string = $item->$find; | |
$href = $url.$string; | |
if (!(strpos($href, 'http://') === 0)) $href = "http://".$href; | |
$replace = substr($text,$item->indices[0],$item->indices[1]-$item->indices[0]); | |
$with = "<a href=\"$href\">{$prefix}{$string}</a>"; | |
$replace_index[$replace] = $with; | |
} | |
} | |
foreach ($replace_index as $replace => $with) $tweet['text'] = str_replace($replace,$with,$tweet['text']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment