我是PHP新手,試圖用下面的代碼中的google.com替換URL模式。在PHP中正則表達式中轉義引號時出錯
$textStr = "Test string contains http://foo.com/more_(than)_one_(parens)
http://foo.com/blah_(wikipedia)#cite-1
http://foo.com/blah_(wikipedia)_blah#cite-1
http://foo.com/unicode_(?)_in_parens
http://foo.com/(something)?after=parens
more urls foo.ca/me some other text";
$pattern = '(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)((?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»「」‘’]))*)';
$textStr = preg_replace($pattern, "google.com", $textStr);
echo $textStr;
我發現正則表達式模式在http://daringfireball.net/2010/07/improved_regex_for_matching_urls但我一直沒能成功逃脫單引號,雙引號中的格局。
目前我得到的消息 - 警告:的preg_replace()未知的修飾詞「\」 但我用斜線(),以逃避{單引號};:\'」
能有人幫我上面?
[將ereg表達式轉換爲preg(缺少分隔符)](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario 2012-01-09 05:56:26