egr103 egr103 - 4 months ago 21
HTML Question

Using PHP and the new Twitter API

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']);

}

Answer

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']));

}