Skip to content

Instantly share code, notes, and snippets.

@issunboshi
Created August 13, 2013 16:36
Show Gist options
  • Save issunboshi/6222994 to your computer and use it in GitHub Desktop.
Save issunboshi/6222994 to your computer and use it in GitHub Desktop.
A Twitter feed snippet that separates out links included amongst text and wraps them in a tags
<?php
@require_once('twitteroauth/twitteroauth.php');
$twitteruser = "@ssetelecoms";
$notweets = 3;
$consumerkey = "kJ1XGV8QkbR411MKrWV6Q";
$consumersecret = "HgRC8zL2dzX0m8HFfr4EdOwe4fmmwWm4Anp3UaJx0";
$accesstoken = "95167909-qSMJwB7cC2Qc0oiS60ruOjgp9iSolklD21F7hTaSp";
$oauth_token_secret = "GoowY6AWcOtCUzyirsZQapvp8FYTnCVE21nNr7v0M";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $auth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $auth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $oauth_token_secret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
foreach ($tweets as $tweet) {
?>
<div class="page-post">
<?php
$count = 0;
$string = $tweet->text;
$pattern = '/http:\/\/t.co\/[a-zA-Z0-9]{10}/';
$tweet_has_url = $tweet->entities->urls[0];
foreach ($tweet->entities->urls as $url) {
$matches[$count] = preg_match($pattern, $string);
// echo $matches[$count];
$count++;
}
$count = 0;
if ($tweet_has_url) {
foreach ($tweet->entities->urls as $url) {
// Need to change this to change more than 1 link in a tweet. Probably use preg_match to find list of matches, then loop through returned array and use preg_replace on each link
$replacement = "<a href='http://".$tweet->entities->urls[$count]->display_url."' class='tweet-link'>".$tweet->entities->urls[$count]->display_url."</a>";
$swapped_content = preg_replace($pattern, $replacement, $string);
$count++;
echo "<p>".$swapped_content."<br>";
}
}
else {
$replacement = "<a href='http://".$tweet->entities->urls[$count]->display_url."' class='tweet-link'>".$tweet->entities->urls[$count]->display_url."</a>";
$swapped_content = preg_replace($pattern, $replacement, $string);
echo "<p>".$swapped_content."<br>";
}
?>
</div>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment