楼主:
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,另一种是把那个字也 () 起来搬进取代字串里