2

Because of the new Twitter API, I am using PHP to display 1 latest tweet on my webpage using PHP.

At the moment I have got it working so that the tweet is just outputted as a simple text string. My question is how do I control the HTML outputted? I want to be able to display the links as links if a hashtag or web address is stated within the tweet. How do I do this?

Here's my code so far that outputs the string as a tweet in my page:

function get_tweet() {

require 'tmhOAuth.php';
require 'tmhUtilities.php';

$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'secret',
'consumer_secret' => 'secret',
'user_token' => 'secret',
'user_secret' => 'secret',
'curl_ssl_verifypeer' => false
));

$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'evanrichards',
'count' => '1'));

$response = $tmhOAuth->response['response'];
$tweets = json_decode($response, true);
echo($tweets[0]['text']);

}
Jimbo
  • 25,790
  • 15
  • 86
  • 131
egr103
  • 3,858
  • 15
  • 68
  • 119
  • Your best bet is to use a regular expression. The regex I use for my website is `/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i` which is replaced with `$1`. You can use the php `preg_replace()` function for this. But my regex only turns url's into links. You would probably need another one for the hash tags. I'll investigate as I am interested in adding them myself. Perhaps a third could be added for `@names` to link to a profile. – Jacob Tomlinson Mar 13 '13 at 13:55

2 Answers2

11

Here is some example code for replacing links, hashtags and attags with links in php

$tweet = "@george check out http://www.google.co.uk #google";

//Convert urls to <a> links
$tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);

//Convert hashtags to twitter searches in <a> links
$tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);

//Convert attags to twitter profiles in <a> links
$tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);

echo $tweet;

This gives the output

<a href="http://www.twitter.com/george">@george</a> check out <a target="_blank" href="http://www.google.co.uk">http://www.google.co.uk</a> <a target="_new" href="http://twitter.com/search?q=google">#google</a>

So you could change your code to

function get_tweet() {

  require 'tmhOAuth.php';
  require 'tmhUtilities.php';

  $tmhOAuth = new tmhOAuth(array(
  'consumer_key' => 'secret',
  'consumer_secret' => 'secret',
  'user_token' => 'secret',
  'user_secret' => 'secret',
  'curl_ssl_verifypeer' => false
  ));

  $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
  'screen_name' => 'evanrichards',
  'count' => '1'));

  $response = $tmhOAuth->response['response'];
  $tweets = json_decode($response, true);

  $tweet = $tweets[0]['text'];
  $tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);
  $tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);
  $tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);
  echo($tweet);

}

I'm sure the regex's could be improved though.

Or even better you can then split it out into it's own function.

function linkify_tweet($tweet) {

  $tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);
  $tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);
  $tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);

  return $tweet;

}

function get_tweet() {

  require 'tmhOAuth.php';
  require 'tmhUtilities.php';

  $tmhOAuth = new tmhOAuth(array(
  'consumer_key' => 'secret',
  'consumer_secret' => 'secret',
  'user_token' => 'secret',
  'user_secret' => 'secret',
  'curl_ssl_verifypeer' => false
  ));

  $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
  'screen_name' => 'evanrichards',
  'count' => '1'));

  $response = $tmhOAuth->response['response'];
  $tweets = json_decode($response, true);

  echo(linkify_tweet($tweets[0]['text']));

}
Jacob Tomlinson
  • 3,341
  • 2
  • 31
  • 62
  • That's awesome! Works a treat! I did comment it didn't work but that was an error on my part. Thanks a bunch! – egr103 Mar 13 '13 at 14:55
6

A simple regular expression will work in most cases. But Twitter entities solve the parsing problem for you, plus give you more information about links, usernames, etc.

See The full documentation here: https://dev.twitter.com/overview/api/entities-in-twitter-objects

To format the text, get a list of all replacements you need to make, then do the replacements in reverse order (so the offsets stay correct after the text is expanded).

<?php
function twitter_format($tweet) {
    $text = $tweet['text'];
    $entities = isset($tweet['entities']) ? $tweet['entities'] : array();

    $replacements = array();
    if (isset($entities['hashtags'])) {
        foreach ($entities['hashtags'] as $hashtag) {
            list ($start, $end) = $hashtag['indices'];
            $replacements[$start] = array($start, $end,
                "<a href=\"https://twitter.com/search?q={$hashtag['text']}\">#{$hashtag['text']}</a>");
        }
    }
    if (isset($entities['urls'])) {
        foreach ($entities['urls'] as $url) {
            list ($start, $end) = $url['indices'];
            // you can also use $url['expanded_url'] in place of $url['url']
            $replacements[$start] = array($start, $end,
                "<a href=\"{$url['url']}\">{$url['display_url']}</a>");
        }
    }
    if (isset($entities['user_mentions'])) {
        foreach ($entities['user_mentions'] as $mention) {
            list ($start, $end) = $mention['indices'];
            $replacements[$start] = array($start, $end,
                "<a href=\"https://twitter.com/{$mention['screen_name']}\">@{$mention['screen_name']}</a>");
        }
    }
    if (isset($entities['media'])) {
        foreach ($entities['media'] as $media) {
            list ($start, $end) = $media['indices'];
            $replacements[$start] = array($start, $end,
                "<a href=\"{$media['url']}\">{$media['display_url']}</a>");
        }
    }

    // sort in reverse order by start location
    krsort($replacements);

    foreach ($replacements as $replace_data) {
        list ($start, $end, $replace_text) = $replace_data;
        $text = mb_substr($text, 0, $start, 'UTF-8').$replace_text.mb_substr($text, $end, NULL, 'UTF-8');
    }
    return "<p>$text</p>";
}

echo twitter_format($tweets[0]);
mcrumley
  • 5,682
  • 3
  • 25
  • 33
  • +1 thanks for this, you might just want to add a check for the presence of hashtags et al, using `if(isset($entities['hashtags'])...` before the foreach loop – schellmax Oct 30 '13 at 08:34
  • i've also had issues with the use of `substr_replace` as the incoming string seemed to be UTF-8. solved by converting to iso-8859-1 before running substr_replace and then back to UTF-8, guess that's not fancy though ;) – schellmax Oct 30 '13 at 14:02
  • Apparently the offset is in characters, not bytes. Instead of converting to iso-8859-1, did you try using the mb_* functions? Something like `$text = mb_substr($text, 0, $start).$replace_text.mb_substr($text, $start+$length);` – mcrumley Oct 30 '13 at 15:11