PHP / Regex - 警告:preg_match_all()[function.preg-match-all]:未知的修饰符

时间:2015-03-18 15:54:48

标签: php regex

伙计们我有这个错误:

Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'E' in /home/sportsdi/public_html/test/index.php on line 17

这是我的代码:

<?PHP
$url = "http://www.sportsdirect.com/donnay-10-pack-trainer-socks-411038?colcode=41103890";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
 $str = curl_exec($curl);  
 curl_close($curl);  


$SIZEID = 'UK: 8-13 Kids / EU: 25-32 Kids';
$occurencies = preg_match_all('/(?<=\"SizeName\":\"'.$SIZEID.'")\S+/i', $str, $match);


echo $occurencies;

问题似乎来自/变量中的$SIZEID符号。 使用此符号进行搜索非常重要。我有什么建议可以解决这个问题吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以像这样简单地使用preg_quote()

(这会转义每个正则表达式字符)

$occurencies = preg_match_all('/(?<=\"SizeName\":\"' . preg_quote($SIZEID, "/") . '")\S+/i', $str, $match);
                                                     //^^^^^^^^^^

答案 1 :(得分:0)

使用preg_quote来处理变量中的转义字符:

preg_match_all('/(?<=\"SizeName\":\"' . preg_quote($SIZEID, '/') . '")\S+/i', $str, $match);

默认情况下,第二个参数是正斜杠 - 它应与您用于开始和结束正则表达式的任何内容相匹配。