Skip to content

Instantly share code, notes, and snippets.

@dmrrlc
Last active November 30, 2015 13:59
Show Gist options
  • Save dmrrlc/6f21334a89389fd2d42e to your computer and use it in GitHub Desktop.
Save dmrrlc/6f21334a89389fd2d42e to your computer and use it in GitHub Desktop.
Parse twitter message
<?php
// Parse a tweet to add links, mentions and hashtags
private function parseTwitterMessage($tweet)
{
foreach($tweet->entities as $entity){
foreach ($entity->urls as $url) {
$tweet->value = str_replace(
$url->url,
'<a href="' . $url->url . '" target="_blank">' . $url->display_url . '</a>',
$tweet->value);
}
foreach ($entity->hashtags as $hashtag) {
$tweet->value = str_replace(
'#' . $hashtag->text,
'<a href="https://twitter.com/hashtag/' . $hashtag->text . '?src=hash" target="_blank">#' . $hashtag->text . '</a>',
$tweet->value);
}
foreach ($entity->user_mentions as $mention) {
$tweet->value = str_replace(
'@' . $mention->screen_name,
'<a href="https://twitter.com/' . $mention->screen_name . '" target="_blank">@' . $mention->screen_name . '</a>',
$tweet->value);
}
//remove media link from $tweet->value
foreach ($entity->media as $media) {
$tweet->value = str_replace(
$media->url,
'',
$tweet->value);
}
}
return $tweet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment