問題描述
我正在編寫一個小的 PHP 腳本,以從用戶提要中獲取最新的半打 Twitter 狀態更新,并將它們格式化以在網頁上顯示.作為其中的一部分,我需要一個正則表達式替換來將主題標簽重寫為 search.twitter.com 的超鏈接.最初我嘗試使用:
(取自 https://gist.github.com/445729)
在測試過程中,我發現 #test 被轉換為 Twitter 網站上的鏈接,但 #123 不是.在互聯網上進行了一些檢查并使用各種標簽后,我得出的結論是,主題標簽必須在某處包含字母字符或下劃線才能構成鏈接;僅包含數字字符的標簽將被忽略(大概是為了阻止諸如好的演示文稿鮑勃,幻燈片 #3 是我的最愛!"之類的鏈接).這使得上面的代碼不正確,因為它很樂意將#123 轉換為鏈接.
我已經有一段時間沒有做太多正則表達式了,所以在我生疏的時候,我想出了以下 PHP 解決方案:
0) {foreach ($arrHashtags[2] as $strHashtag) {//檢查每個標簽,看看某處是否有字母或下劃線if (preg_match('/#d*[a-z_]+/i', $strHashtag)) {$test = str_replace($strHashtag, '<a .substr($strHashtag, 1).'">'.$strHashtag.'</a>', $test);}}}回聲 $test;?>
它有效;但它的作用似乎相當冗長.我的問題是,是否有一個類似于我從 gist.github 獲得的 preg_replace 將有條件地將主題標簽重寫為超鏈接,前提是它們不只包含數字?
(^|s)#(w*[a-zA-Z_]+w*)
PHP
$strTweet = preg_replace('/(^|s)#(w*[a-zA-Z_]+w*)/', '1#<a , $strTweet);
此正則表達式表示 # 后跟 0 個或多個字符 [a-zA-Z0-9_],后跟字母字符或下劃線(1 個或多個),然后是 0 個或多個單詞字符.
http://rubular.com/r/opNX6qC4sG <- 在此處測試.>
I'm writing a small PHP script to grab the latest half dozen Twitter status updates from a user feed and format them for display on a webpage. As part of this I need a regex replace to rewrite hashtags as hyperlinks to search.twitter.com. Initially I tried to use:
<?php
$strTweet = preg_replace('/(^|s)#(w+)/', '1#<a , $strTweet);
?>
(taken from https://gist.github.com/445729)
In the course of testing I discovered that #test is converted into a link on the Twitter website, however #123 is not. After a bit of checking on the internet and playing around with various tags I came to the conclusion that a hashtag must contain alphabetic characters or an underscore in it somewhere to constitute a link; tags with only numeric characters are ignored (presumably to stop things like "Good presentation Bob, slide #3 was my favourite!" from being linked). This makes the above code incorrect, as it will happily convert #123 into a link.
I've not done much regex in a while, so in my rustyness I came up with the following PHP solution:
<?php
$test = 'This is a test tweet to see if #123 and #4 are not encoded but #test, #l33t and #8oo8s are.';
// Get all hashtags out into an array
if (preg_match_all('/(^|s)(#w+)/', $test, $arrHashtags) > 0) {
foreach ($arrHashtags[2] as $strHashtag) {
// Check each tag to see if there are letters or an underscore in there somewhere
if (preg_match('/#d*[a-z_]+/i', $strHashtag)) {
$test = str_replace($strHashtag, '<a .substr($strHashtag, 1).'">'.$strHashtag.'</a>', $test);
}
}
}
echo $test;
?>
It works; but it seems fairly long-winded for what it does. My question is, is there a single preg_replace similar to the one I got from gist.github that will conditionally rewrite hashtags into hyperlinks ONLY if they DO NOT contain just numbers?
(^|s)#(w*[a-zA-Z_]+w*)
PHP
$strTweet = preg_replace('/(^|s)#(w*[a-zA-Z_]+w*)/', '1#<a , $strTweet);
This regular expression says a # followed by 0 or more characters [a-zA-Z0-9_], followed by an alphabetic character or an underscore (1 or more), followed by 0 or more word characters.
http://rubular.com/r/opNX6qC4sG <- test it here.
這篇關于正則表達式有條件地用超鏈接替換 ??Twitter 標簽的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!