Skip to content

Instantly share code, notes, and snippets.

@bertrandom
Created November 29, 2011 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bertrandom/1405715 to your computer and use it in GitHub Desktop.
Save bertrandom/1405715 to your computer and use it in GitHub Desktop.
un t.co function
<?php
public function getLinkifiedText() {
$text = $this->getTweetText();
$entities = new \stdClass;
if ($urls = $this->getEntitiesUrls()) {
$urls = unserialize($urls);
$entities->urls = $urls;
} else {
$entities->urls = array();
}
if ($user_mentions = $this->getEntitiesUserMentions()) {
$user_mentions = unserialize($user_mentions);
$entities->user_mentions = $user_mentions;
} else {
$entities->user_mentions = array();
}
if ($hashtags = $this->getEntitiesHashtags()) {
$hashtags = unserialize($hashtags);
$entities->hashtags = $hashtags;
} else {
$entities->hashtags = array();
}
if ($media = $this->getEntitiesMedia()) {
$media = unserialize($media);
$entities->media = $media;
} else {
$entities->media = array();
}
return $this->linkify($text, $entities);
}
#
# I modified this linkify script to actually work with UTF-8 data
# http://140dev.com/free-twitter-api-source-code-library/twitter-display/linkify-php/
#
const HASHTAG_TITLE = 'View tweets for tag: ';
const HASHTAG_URL = 'http://search.twitter.com/search?q=%23';
const USER_MENTION_TITLE = 'View tweets by user: ';
const USER_MENTION_URL = 'http://twitter.com/';
public function linkify($tweet_text, $entities) {
$mb_encoding_stack = mb_internal_encoding();
mb_internal_encoding("UTF-8");
// $entities is an object delivered by the Twitter API for each tweet with
// the user @mentions, hastags, and URLs broken out along with their positions
// Create an array of entities with the starting point of the entity as the key
// This will allow the processing of the tweet in a character by character loop
// Entities can be replaced by their hyperlink versions within this loop
$entity_map = array();
// Extract user mentions
foreach ($entities->user_mentions as $user_mention) {
$start = $user_mention->indices[0];
$entity_map[$start] = array('screen_name'=> $user_mention->screen_name,
'name' => $user_mention->name,
'indice_end' => $user_mention->indices[1],
'type' => 'user_mention' );
}
// Extract hashtags
foreach ($entities->hashtags as $hashtag) {
$start = $hashtag->indices[0];
$entity_map[$start] = array('text'=> $hashtag->text,
'indice_end' => $hashtag->indices[1],
'type' => 'hashtag');
}
// Extract URLs
foreach ($entities->urls as $url) {
$start = $url->indices[0];
$hash = array('url'=> $url->url,
'expanded_url'=> $url->expanded_url,
'type' => 'url',
'indice_end' => $url->indices[1],
);
if (isset($url->display_url)) {
$hash['display_url'] = $url->display_url;
}
$entity_map[$start] = $hash;
}
// Extract URLs
foreach ($entities->media as $url) {
$start = $url->indices[0];
$hash = array('url'=> $url->url,
'expanded_url'=> $url->expanded_url,
'type' => 'url',
'indice_end' => $url->indices[1],
);
if (isset($url->display_url)) {
$hash['display_url'] = $url->display_url;
}
$entity_map[$start] = $hash;
}
// Loop through the tweet text one character at a time
$charptr = 0;
$text_end = mb_strlen($tweet_text) - 1;
// Construct a new version of the text with entities converted to links
$new_text = '';
while ($charptr <= $text_end) {
// Does the current character have a matching element in the $entity_map array?
if (isset($entity_map[$charptr])) {
switch ($entity_map[$charptr]['type']) {
case 'user_mention':
$new_text .= '<a href="' . self::USER_MENTION_URL .
$entity_map[$charptr]['screen_name'] .
'" title="' . self::USER_MENTION_TITLE .
$entity_map[$charptr]['screen_name'] .
' (' . $entity_map[$charptr]['name'] . ')">@' .
$entity_map[$charptr]['screen_name'] . '</a>';
$charptr = $entity_map[$charptr]['indice_end'];
break;
case 'hashtag':
$new_text .= '<a href="' . self::HASHTAG_URL .
$entity_map[$charptr]['text'] .
'" title="' . self::HASHTAG_TITLE .
$entity_map[$charptr]['text'] . '">#' .
$entity_map[$charptr]['text'] . '</a>';
$charptr = $entity_map[$charptr]['indice_end'];
break;
case 'url':
$new_text .= '<a href="';
if ($entity_map[$charptr]['expanded_url']) {
$new_text .= $entity_map[$charptr]['expanded_url'];
} else {
$new_text .= $entity_map[$charptr]['url'];
}
if (isset($entity_map[$charptr]['display_url'])) {
$new_text .= '">' . $entity_map[$charptr]['display_url'] . '</a>';
} else {
$new_text .= '">' . $entity_map[$charptr]['url'] . '</a>';
}
$charptr = $entity_map[$charptr]['indice_end'];
break;
}
} else {
$new_text .= mb_substr($tweet_text,$charptr,1);
++$charptr;
}
}
mb_internal_encoding($mb_encoding_stack);
return $new_text;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment