Question

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;
    ?>
Was it helpful?

Solution

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;
?>

OTHER TIPS

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;
?>

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']);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top