在PHP中转义正则表达式中的引号时出错


Error escaping quotes in regular expressions in PHP

我是PHP的新手,正在尝试用下面的代码中的google.com替换URL模式。

    $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()未知修饰符"''"但我使用了slash()来转义{};:'''中的单引号"

有人能帮我处理上面的代码吗?

对于preg_replace,首先必须用/来分隔正则表达式,如:

/'b((?:https: ... etc etc)/

其次,由于您用/分隔正则表达式,因此必须用反斜杠转义任何/。所以https://->https:'/'/

第三,你的修饰符(?i)在后面的斜线后面:

`/'b((?:https: .. etc etc)/i`

Try(所做的更改:转义/,将正则表达式从(?i)regex移动到/regex/i):

$pattern = '/'b((?:https?:'/'/|www'd{0,3}[.]|[a-z0-9.'-]+[.][a-z]{2,4}'/)((?:[^'s()<>]+|'(([^'s()<>]+|('([^'s()<>]+')))*'))+(?:'(([^'s()<>]+|('([^'s()<>]+')))*')|[^'s`!()'[']{};:''".,<>?«»“”‘’]))*)/i';
$textStr = preg_replace($pattern, "google.com", $textStr); 
echo $textStr;

现在,由于$pattern与整个URL匹配,您只需退出:

"Test string contains google.com
google.com
google.com
google.com
google.com
more urls google.com some other text"

因此,总的来说,我建议@Ampere的答案(但它的正则表达式比原来的要宽松),或者使用捕获括号和backreferences来执行类似preg_replace($pattern,'google.com/'2',$textStr)的操作(但要适当修改捕获括号,因为这不适用于当前的捕获括号排列)。

这个网站对测试很有用。

$patterrn='/([wW]{3,3}'.|)[A-Za-z0-9]+?'./';
$text="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";
$output = preg_replace($patterrn,"abc.",$text);
print_r($output);

输出为

Test string contains http://abc.com/more_(than)_one_(parens) http://abc.com/blah_(wikipedia)#cite-1 http://abc.com/blah_(wikipedia)_blah#cite-1 http://abc.com/unicode_(?)_in_parens http://abc.com/(something)?after=parens more urls abc.ca/me some other text