Skip to content

Instantly share code, notes, and snippets.

@joshuabaker
Last active December 23, 2015 09:09
Show Gist options
  • Save joshuabaker/6612623 to your computer and use it in GitHub Desktop.
Save joshuabaker/6612623 to your computer and use it in GitHub Desktop.
Switches tweet entities, from a Twitter API response, into anchor links, returning HTML ready for display. Built for use with https://github.com/joshuabaker/twitter-proxy
function get_tweet_html($tweet)
{
$html = $tweet->text;
if (empty($tweet->entities))
{
return $html;
}
$entities = array();
foreach ($tweet->entities->hashtags as $entity)
{
$entities[$entity->indices[0]] = array(
$entity->indices[1] - $entity->indices[0],
"<a href=\"https://twitter.com/search?q=%23{$entity->text}\" target=\"_blank\">#{$entity->text}</a>",
);
}
foreach ($tweet->entities->user_mentions as $entity)
{
$entities[$entity->indices[0]] = array(
$entity->indices[1] - $entity->indices[0],
"<a href=\"https://twitter.com/{$entity->screen_name}\" target=\"_blank\">@{$entity->screen_name}</a>",
);
}
foreach ($tweet->entities->urls as $entity)
{
$target = '';
preg_match('/https?:\/\/([^\/]*).*/i', $entity->expanded_url, $match);
if ($match && str_replace('www.', '', $match[1]) != str_replace('www.', '', gethostname()))
{
$target = ' target="_blank"';
}
$entities[$entity->indices[0]] = array(
$entity->indices[1] - $entity->indices[0],
"<a href=\"{$entity->expanded_url}\"{$target}>{$entity->display_url}</a>",
);
}
foreach ($tweet->entities->media as $entity)
{
$entities[$entity->indices[0]] = array(
$entity->indices[1] - $entity->indices[0],
"<a href=\"{$entity->media_url_https}\" target=\"_blank\">{$entity->display_url}</a>",
);
}
$entities = array_reverse($entities, true);
foreach ($entities as $start => $entity)
{
$html = substr_replace($html, $entity[1], $start, $entity[0]);
}
return $html;
}
var getTweetHTML = function(tweet)
{
var i, item, entities = {}, html = tweet.text, target;
for (i in tweet.entities.hashtags)
{
item = tweet.entities.hashtags[i];
entities[item.indices[0]] = [item.indices[1], '<a href="https://twitter.com/search?q=%23' + item.text + '" target="_blank">#' + item.text + '</a>'];
}
for (i in tweet.entities.user_mentions)
{
item = tweet.entities.user_mentions[i];
entities[item.indices[0]] = [item.indices[1], '<a href="https://twitter.com/' + item.screen_name + '" target="_blank">@' + item.screen_name + '</a>'];
}
for (i in tweet.entities.urls)
{
item = tweet.entities.urls[i];
target = !item.expanded_url.replace(/https?:\/\/([^\/]*).*/i, '$1').match(location.hostname) ? ' target="_blank"' : '';
entities[item.indices[0]] = [item.indices[1], '<a href="' + item.expanded_url + '"' + target + '>' + item.display_url + '</a>'];
}
for (i in tweet.entities.media)
{
item = tweet.entities.media[i];
entities[item.indices[0]] = [item.indices[1], '<a href="' + item.media_url_https + '" target="_blank">' + item.display_url + '</a>'];
}
// Loop backwards through the tweet text string, which is what we need and also happens to be faster
for (i = html.length - 1; i >= 0; i--)
{
if (entities[i])
{
html = html.substring(0, i) + entities[i][1] + html.substring(entities[i][0]);
}
}
return html;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment