Wrap link around links in tweets with php preg_replace
        Posted  
        
            by 
                Ben Paton
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ben Paton
        
        
        
        Published on 2010-11-25T02:31:34Z
        Indexed on 
            2010/12/25
            20:54 UTC
        
        
        Read the original article
        Hit count: 382
        
Hello I'm trying to display the latest tweet using the code below. This preg_replace works great for wrapping a link round twitter @usernames but doesn't work for web addresses in tweets. How do I get this code to wrap links around urls in tweets.
        <?php
        /** Script to pull in the latest tweet */
        $username='fairgroceruk';
        $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('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
        $latestTweet = preg_replace('/http://([a-z0-9_]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet); echo $latestTweet;
    ?>
Thanks for the help,
Ben
© Stack Overflow or respective owner