我有下面的代碼解析Twitter文字修改鏈接,提到和哈希爲超鏈接:解析Twitter文字使用PHP的preg_replace
function parseTwitterText($text) {
$returnText = $text;
$hashPattern = '/\#([A-Za-z0-9\_]+)/i';
$mentionPattern = '/\@([A-Za-z0-9\_]+)/i';
$urlPattern = '/(http[s]?\:\/\/[^\s]+)/i';
$robotsFollow = false;
// SCAN FOR LINKS FIRST!!! Otherwise it will replace the hashes and mentions
$returnText = preg_replace($urlPattern, '\<a href\="$1" ' + (($robotsFollow)? '':'rel\=\"nofollow\"') + '\>$1\<\/a\>', $returnText);
$returnText = preg_replace($hashPattern, '\<a href\="http\:\/\/twitter\.com\/\#\!\/search\?q\=\%23$1" ' + (($robotsFollow)? '':'rel\=\"nofollow\"') + '\>\#$1\<\/a\>', $returnText);
$returnText = preg_replace($mentionPattern, '\<a href\="http\:\/\/twitter\.com\/$1" ' + (($robotsFollow)? '':'rel\=\"nofollow\"') + '\>@$1\<\/a\>', $returnText);
return $returnText;
}
不過我剛開始如果我有像鳴叫0
返回#test
我基於JavaScript版本我有這個代碼,所以想知道如果我做錯了什麼在preg_replace()
,只有在JS replace()
以冷凝PHP字符串使用'.',而不是'+',你在JS會。 – user555