开发者

Output @ and # as normal text on Twitter feed

I'm outputting a Twitter feed using the variable $tweet, when it outputs the tweet it stops when it hits an @ or hastag, is it possible to just output those as normal text?

E.g

This is what happens when it tries to show a @

(i.e as soon as it hits the @ it stops, the same applies to a hashtag)

UPDATE:

<?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]->tex开发者_JS百科t);
$newTweet = substr($theTweet,0,65);
echo '<a class="tweet" rel="nofollow" href="http://www.twitter.com/teamworksdesign"> "' . $newTweet . '..."</a>';

?>


EDIT: The answer is in the comments.

Some parts of that function appear to be unnecessary. You could have a go with this function instead, as I know this one works?

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;
}


Now - not knowing exactly where your problem is, this is what i do when i am reading a tweet to display as 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>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜