I tried adding your code:
Code: Select all
//Link @replies to their profile
preg_match('/@[a-z0-9]+/',$tweet,$matches);
$repl = substr($matches[0],1);
$tweet = preg_replace('/'.$repl.'/i','<a href="http://www.twitter.com/'.$repl.'" target="_blank">'.$repl.'</a>',$tweet);
$repl = substr($matches[1],1);
$tweet = preg_replace('/'.$repl.'/i','<a href="http://www.twitter.com/'.$repl.'" target="_blank">'.$repl.'</a>',$tweet);
I ended up with this:
and on through every character of the string. I would expect this to happen when trying to replace by a blank string - the replacement is put between every character. The string I used to test this was "test @person1 @person2 blah".
On the page all you see is the tweet and the html of what should be the link. If you look at the source you can see the above.
Edit:
Okay I seem to have solved it now, by using preg_match_all rather than preg_match. Thank you for all the help you gave. The working code is:
Code: Select all
<?PHP
function GetLastTweet($user)
{
$tweet = 'Error obtaining tweet';
$co = curl_init('http://twitter.com/statuses/user_timeline/'.$user.'.xml');
curl_setopt($co, CURLOPT_VERBOSE, 1);
curl_setopt($co, CURLOPT_HEADER, 0);
curl_setopt($co, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($co, CURLOPT_RETURNTRANSFER, 1);
$cr = curl_exec($co);
$ci = curl_getinfo($co);
curl_close($co);
if($ci['http_code']==200)
{
//Get latest tweet
preg_match('/<text>(.*?)<\/text>/',$cr,$matches);
$tweet = $matches[1];
//Break at 84 characters into a two lines
$tweet = wordwrap($tweet,84,'<br/>',false);
//Link @replies to their profile
preg_match_all('/@[a-z0-9]+/',$tweet,$matches);
foreach($matches as $match)
{
foreach($match as $m)
{
$repl = substr($m,1);
$tweet = preg_replace('/'.$repl.'/i','<a href="http://www.twitter.com/'.$repl.'" target="_blank">'.$repl.'</a>',$tweet);
}
}
}
else
{
//Break at 84 characters into a two lines
$tweet = wordwrap($tweet,84,'<br/>',false);
}
return $tweet;
}
?>