I have a long long $partner list, write in a single common file.

remove.inc

$partner =<<<EOT
"#<h1 class=\"logo\"(.*?)</h1>#s","#<h2 class=\"hidden\"(.*?)</h2>#s"
EOT;
//more $partner rules...

index.php

include(remove.inc);
$str = preg_replace(array($partner), '', $str);

this return:

Warning: preg_replace(): Unknown modifier ',' in d:\www\indoor\index.php on line 12

有帮助吗?

解决方案

$partner is supposed to be an array of strings, but it's not: you are defining it as a string using HEREDOC syntax.

The first character of the string is the double quote, which the regex engine treats as the delimiter. Therefore when the next unescaped double quote is encountered:

"#<h1 class=\"logo\"(.*?)</h1>#s",
                                ^

the engine assumes that whatever follows must be regex modifiers. Since , is not a valid modifier the result is an error.

The correct way to do it is

$partner = array(
    "#<h1 class=\"logo\"(.*?)</h1>#s",
    "#<h2 class=\"hidden\"(.*?)</h2>#s",
);

and used as

$str = preg_replace($partner, '', $str);

其他提示

Your heredoc strings will include the quotes and the commas when defined like in your example. This will have the effect of your regular exceptions being actually delimited by the quotes, instead of the hash marks, thus the first regular exception ends after #s" and what follows is a comma, which as your error message states, is not a valid regular exception flag. There is also nothing that would split your heredoc expressions into separate elements in an array — array($partner) would result in an array with a single element containing your entire heredoc string.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top