Php 将@和#输出为Twitter提要上的正常文本

Php 将@和#输出为Twitter提要上的正常文本,php,twitter,Php,Twitter,我使用变量$tweet输出Twitter提要,当它输出tweet时,在点击@或hastag时停止,是否可以将其作为普通文本输出 例如 (即,一旦它点击@it stops,这同样适用于hashtag) 更新: <?php function parseTweet($text) { $pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-

我使用变量
$tweet
输出Twitter提要,当它输出tweet时,在点击@或hastag时停止,是否可以将其作为普通文本输出

例如

(即,一旦它点击@it stops,这同样适用于hashtag)

更新:

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

?>

编辑:答案在评论中。

该功能的某些部分似乎是不必要的。你可以试试这个函数,我知道这个可以用吗

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;
}
函数解析tweet($ret){
$ret=preg#u replace(“#(^|[\n])([\w]+?:/[\w]+[^\”\n\r\t<]*)#“,“\\1”,$ret);
$ret=preg#u replace(“#(^|[\n])((www.ftp)\.[^\”\t\n\r<]*)#“,“\\1”,$ret);
$ret=preg_replace(“/@(\w+/”,“”,$ret);//用户名
$ret=preg#u replace(“/#(\w+/”,“”,$ret);//散列标记
返回$ret;
}

现在-不知道您的问题在哪里,当我阅读一条tweet以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>";
//从tweet中过滤用户的用户名
$tweet=str_replace($username.:“,”“,$tweet);
//将URL转换为超链接
$tweet=preg\u replace(“/(http:\/\/)(.*?\/”([\w\.\/\&=\?\-\,\:\\\\\\\\\\\\\%\+]*)/”,“,”,$tweet);
//链接到回复中的用户
$tweet=preg_replace(“(@([a-zA-Z0-9\\]+)”,”,$tweet);
//添加发布的时间
$tweet=$tweet。" " . $张贴。"";

您是如何输出的?我猜是PHP?有点难猜!你介意也发布你的代码吗?@diagonalbatman我已经用代码更新了我的问题。现在我假设你的错误在parseTweet()函数中的某个地方。你能确认你在哪里丢失了字符串的剩余部分吗。。。?您的正则表达式可能有问题。谢谢,尽管这也会切断@的连接。是因为您有substr($theweet,0,65)吗?这似乎很奇怪,因为推特超过65个字符?
// 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>";