Ben Paton
Ben Paton

Reputation: 1442

Turn URLs into links when showing the latest tweet from a Twitter account with preg_replace

I have cobbled together the code block below to help me show the latest tweet on my website from my Twitter account. However, it's not quite working right, can you help me debug this last little bit. I am looking for the PHP to turn it to HTML with link tags wrapped around the Twitter usernames and links which it is doing using preg_replace.

If you test this script you'll see there is a problem with when it renders out standard links in tweets it puts the closing <a> tag in too early after a. I'm sure this is relatively simple to fix and probably about escaping characters or something.

My main codeblock:

    <?php
        /** Script to pull in the latest tweet */
        $username='benpaton';
        $format = 'json';
        $tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
        $latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
        $latestTweet = preg_replace('/http:\/\/([[a-z0-9_\.\-\+\&\!\#\~\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
        $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
        echo $latestTweet;
    ?>

Upvotes: 2

Views: 2702

Answers (3)

Jonny Nott
Jonny Nott

Reputation: 326

All of the code is buggy or incomplete! What you want to do is something like:

$tweets[$i]['text_html'] = htmlspecialchars($tweet['text']);
$tweets[$i]['text_html'] = preg_replace('%(http://([a-z0-9_.+&!#~/,\-]+))%i','<a href="http://$2">$1</a>',$tweets[$i]['text_html']);
$tweets[$i]['text_html'] = preg_replace('/@([a-z0-9_]+)/i','<a href="http://twitter.com/$1">@$1</a>',$tweets[$i]['text_html']);

Upvotes: 0

James Williams
James Williams

Reputation: 4216

Change your regular expression to:

$latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);

This worked for me.

Full code

<?php
    /** Script to pull in the latest tweet */
    $username='benpaton';
    $format = 'json';
    $tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
    $latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
    $latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
    $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
    echo $latestTweet;
?>

Upvotes: 6

Denis Brat
Denis Brat

Reputation: 497

Try this:

<?php
/** Script to pull in the latest tweet */
$username='benpaton';
$format = 'json';
$tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
$latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
$latestTweet = preg_replace('%http://[a-z0-9_.+&!#~/,\-]+%', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
$latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
echo $latestTweet;
?>

Upvotes: 0

Related Questions