Skip to content

Instantly share code, notes, and snippets.

@anatomic
Created November 13, 2011 20:32
Show Gist options
  • Save anatomic/1362636 to your computer and use it in GitHub Desktop.
Save anatomic/1362636 to your computer and use it in GitHub Desktop.
Processing entities to linkify a tweet
<?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