【发布时间】:2012-08-03 06:17:36
【问题描述】:
您好,我正在尝试使用下面的代码显示最新的推文。这个 preg_replace 非常适合将链接包裹在 twitter @usernames 周围,但不适用于推文中的网址。如何获取此代码以将链接包装在推文中的 url 周围。
<?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;
?>
感谢您的帮助,
本
【问题讨论】:
标签: php twitter preg-replace