我正在使用变量$tweet
输出Twitter Feed,当它输出它在点击@或hastag时停止的推文时,是否可以将它们作为普通文本输出?
E.g
This is what happens when it tries to show a @
(即一旦它到达@它停止,这同样适用于标签)
更新:
<?php
function parseTweet($text) {
$pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
'@([A-Za-z0-9_]+)';
$tweet = preg_replace('/(^|\s)#(\w+)/', '\1#<a
href="http://search.twitter.com/search?q=%23\2″ rel="nofollow">\2</a>', $text);
$tweet = preg_replace('/(^|\s)@(\w+)/', '\1@<a
href="http://www.twitter.com/\2″ rel="nofollow">\2</a>', $tweet);
$tweet = preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is", "\\1<a
href=\"\\2\" title=\"\\2\" rel=\"nofollow\">[link]</a>", $tweet);
return $tweet;
}
$username='teamworksdesign'; // set user name
$format='json'; // set format
$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); // get tweets and decode them into a variable
$theTweet = parseTweet($tweet[0]->text);
$newTweet = substr($theTweet,0,65);
echo '<a class="tweet" rel="nofollow" href="http://www.twitter.com/teamworksdesign"> "' . $newTweet . '..."</a>';
?>
答案 0 :(得分:1)
编辑:答案在评论中。
该功能的某些部分似乎是不必要的。你可以改用这个功能,因为我知道这个有用吗?
function parseTweet($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret); // Usernames
$ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret); // Hash Tags
return $ret;
}
答案 1 :(得分:0)
现在 - 不知道问题究竟在哪里,这就是我在阅读推文显示为HTML时所做的事情:
// filter the user's username out of tweets
$tweet = str_replace($username . ": ", "", $tweet);
// turn URLs into hyperlinks
$tweet = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/", "<a href=\"\\0\">Link</a>", $tweet);
// link to users in replies
$tweet = preg_replace("(@([a-zA-Z0-9\_]+))", "<a href=\"http://www.twitter.com/\\1\">\\0</a>", $tweet);
// add the time posted
$tweet = $tweet . " <span class=\"tweetwhen\">" . $posted . "</span>";