Last active
November 30, 2015 13:59
-
-
Save dmrrlc/6f21334a89389fd2d42e to your computer and use it in GitHub Desktop.
Parse twitter message
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 | |
// 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