作者:
KC73 (肯先生)
2014-01-06 21:40:20各位好:
使用的是 PHP 5.3.27,要抓出文中的以 http 開頭的連結,但是如果 http 前面是 ],
就不抓:
$text = <<<EOT
12345http://www.google.com
http://www.google.com
]http://www.google.com
EOT;
echo preg_replace("![^\]](https?:/{2}[\w\.]{2,}[/\w\-\.\?\&\=\#\~\%]*)!i",
"<a href=\"$1\" title=\"$1\" target=\"_blank\">$1</a>", $text);
結果:
1234<a href="http://www.google.com" title="http://www.google.com"
target="_blank">http://www.google.com</a><a href="http://www.google.com"
title="http://www.google.com" target="_blank">http://www.google.com</a>
]http://www.google.com
第一行的「12345http://www.google.com」的「5」被吃掉了,不知為何?
謝謝~
作者:
LPH66 (-6.2598534e+18f)
2014-01-06 21:44:00但取代字串裡沒有, 所以就被吃掉了兩種解法, 一種是 lookbehind assertion,另一種是把那個字也 () 起來搬進取代字串裡