문제

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