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

时间:2012-01-09 05:53:14

标签: php regex escaping preg-replace

我是PHP的新手,并尝试在下面的代码中使用google.com替换网址格式。

    $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()未知修饰符'\' 但我使用斜杠()来逃避{};中的单引号:\'“

有人可以帮我解决上面的代码吗?

2 个答案:

答案 0 :(得分:1)

$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

答案 1 :(得分:1)

首先preg_replace您必须按/分隔正则表达式,如:

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

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

第三,你的修饰符(?i)追踪尾随斜杠:

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

尝试(所做的更改:转发/,将正则表达式从(?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与整个网址匹配,您只需出去:

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

总而言之,我推荐@ Ampere的答案(但这比你的原始版本更宽松),或使用捕获括号和反向引用来做preg_replace($pattern,'google.com/\2',$textStr)之类的事情(但是要适当修改你的捕获括号,如同这对你当前的捕捉支架安排不起作用。)

This site可用于测试内容。