Created
November 13, 2011 20:32
-
-
Save anatomic/1362636 to your computer and use it in GitHub Desktop.
Processing entities to linkify a tweet
This file contains 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 | |
public function getLinkifiedText(){ | |
$allEntities = array(); | |
foreach(json_decode($this->entities) as $entityGroup){ | |
foreach($entityGroup as $entity){ | |
$allEntities[$entity->indices[0]] = $entity; | |
} | |
} | |
krsort($allEntities); | |
$text = $this->text; | |
foreach($allEntities as $entity){ | |
if(isset($entity->display_url)){ | |
$link = '<a href="'. $entity->url .'" class="tweet-entity" target="_Blank">'.$entity->display_url.'</a>'; | |
} | |
else if(isset($entity->screen_name)){ | |
$link = '<a href="http://www.twitter.com/'.$entity->screen_name.'" class="user-mention-entity" target="_Blank">@'.$entity->screen_name.'</a>'; | |
} | |
else{ | |
$link = '<a href="http://www.twitter.com/#!/search/realtime/'. $entity->text .'" class="hashtag-entity" target="_Blank">'.$entity->text.'</a>'; | |
} | |
$text =substr_replace($text, $link, $entity->indices[0], $entity->indices[1] - $entity->indices[0]); | |
} | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment