Twitter "@" and "#" linking in PHP
Twitter "@" and "#" linking in PHP
我试图转换所有@和#链接时,将代码放入我的网站。
我发现了一些Jquery代码,看起来使用正则表达式搜索@和#,但我不确定如何使用PHP完成同样的事情。
代码是:
at: function(tweet) {
return tweet.replace(/'B[@@]([a-zA-Z0-9_]{1,20})/g, function(m, username) {
return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' + username + '">@' + username + '</a>';
});
},
hash: function(tweet) {
return tweet.replace(/(^|'s+)#('w+)/gi, function(m, before, hash) {
return before + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + hash + '">#' + hash + '</a>';
});
},
如果我有这样的东西:
@BobBarker is going to #blahblah and also @BillyBob
我需要搜索并执行以下操作:
<a target="_blank"
class="twtr-atreply"
href="http://twitter.com/intent/user?screen_name=BobBarker'">@BobBarker
</a>
<a target="_blank"
class="twtr-atreply"
href="http://twitter.com/intent/user?screen_name=BillyBob'">@BillyBob
</a>
对于散列标签也是如此:
#blahblah
我需要搜索并执行以下操作:
<a target="_blank"
class="twtr-hashtag"
href="http://twitter.com/search?q=%23blahblah">#blahblah
</a>
任何帮助将是伟大的!
我把一些PHP放在一起,可以很好地查找第一个瞬间@或#,但不会继续循环。我该如何为它建立一个循环?
$theTweet = "@BobBarker is going to #blahblah and also @BillyBob";
if (preg_match("/'B[@@]([a-zA-Z0-9_]{1,20})/", $theTweet, $matches)) {
$theTweet = str_replace($matches[0],'<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' . trim($matches[0]) . '"> ' . trim($matches[0]) . '</a> ',$theTweet);
echo $theTweet . '<br />';
}
if (preg_match("/(^|'s+)#('w+)/", $theTweet, $matches)) {
$theTweet = str_replace($matches[0],'<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' . trim($matches[0]) . '"> ' . trim($matches[0]) . '</a> ',$theTweet);
echo $theTweet;
}
更新# 2
回答了我自己的问题:0)
如下:
$theTweet = "@BobBarker is going to #blahblah and also @BillyBob";
preg_match_all("@(https?://([-'w'.]+)+(:'d+)?(/(['w/_'.]*('?'S+)?)?)?)@", $theTweet, $matches); //Take care of the http's
foreach($matches[0] as $tweetsHTTP){
$theTweet = str_replace($tweetsHTTP,'<a target="_blank" class="js-display-url" href="' . $tweetsHTTP . '"> ' . $tweetsHTTP . '</a> ', $theTweet);
}
preg_match_all("/'B[@@]([a-zA-Z0-9_]{1,20})/", $theTweet, $matches); //Take care of the @'s
foreach($matches[0] as $tweetsAT){
$theTweet = str_replace($tweetsAT,'<a target="_blank" class="twtr-atreply" href="http://twitter.com/' . str_replace("@", "", $tweetsAT) . '"> ' . $tweetsAT . '</a> ', $theTweet);
}
preg_match_all("/(^|'s+)#('w+)/", $theTweet, $matches); //Take care of the #'s
foreach($matches[0] as $tweetsHash){
$theTweet = str_replace($tweetsHash,'<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' . $tweetsHash . '&src=hash"> ' . $tweetsHash . '</a> ', $theTweet);
}
http://php.net/preg_replace应该能解决你的问题。
像这样(未测试):
preg_replace("/ @(['w'd]+) /i", "<a href='"http://twitter.com/$1/'">@$1</a>", $tweet);
也许您可以将jQuery的相同行为包装到php函数中。大概是这样的。
function writeLinks($tweet){
//for mentions
$tweet = preg_replace("/'B[@]([a-zA-Z0-9_]{1,20})/i", '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name'=$1">$1</a>',$tweet);
//for hashtags
$tweet = preg_replace("/(^|'s+)#('w+)/i", '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search'?q=%23$1>#$1</a>', $tweet);
return $tweet;
}
jQuery脚本使用的是一个简单的正则表达式,在tweet中搜索@和#。您可以使用PHP中的正则表达式函数做类似的事情。
preg_replace函数的作用与示例中的javascript函数大致相同。
function tweetReplaceAt($tweet) {
return preg_replace("/'B[@@]([a-zA-Z0-9_]{1,20})/",'<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=$1">@$1</a>',$tweet);
}
function tweetReplaceHash($tweet) {
return preg_replace("/(^|'s+)#('w+)/i",'<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23$1">#$1</a>',$atreplacedTweet);
}
然后你可以在你的推特上使用这些功能。
preg_replace
还接受第四个参数来限制替换的数量,第五个参数将包含函数调用后的替换数量。
相关文章:
- Node.js's Buffer.writeFloatBE in Javascript
- 铬:“;未捕获的语法错误:意外的标记:"
- Setting default onclick behavior for <img> tag in gene
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 使用“+="操作人员
- Lucene Search in Alfresco
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- Javascript复选框函数:;缺少:在属性id之后"
- "“;变量未引用正确的对象
- "日期“;AJAX请求返回的类型值未定义
- grep in JQuery to C#
- "对于“in”;在Javascript中;未定义的“;价值
- What does 'data()' do in '$("#myWidget"
- HTML select.value in"旧浏览器"
- 如何使用“Google Sign In"”检查用户是否登录;(OAuth 2.0)