这是一个字符串:
$test = '<a id="test">One & -Two - Three</a>';
我想将这两个字符串分成两个不同的变量,例如:
$string1 = 'One & -Two';
和
$string2 = 'Three';
所以我使用了preg_match_all,但是正则表达式有问题:
preg_match_all('#([-;\w\ \.\/\'\d\(\)\&]+)+ - ([\w+\ \.\-]+)+#', $test, $matches);
有人可以解释一下为什么它不起作用..?我看不到我不尊重哪个“规则”。
第一个+
after )
(([-;\w\ \.\/\'\d\(\)\&]+)+
)会导致灾难性的回溯问题(请参阅此处的更多信息),因为这种(a+)+
类型的模式不是结尾子模式。删除+
已经解决了问题。
最后一个子模式具有相同的问题,但不会由于内部PCRE优化而引起麻烦。
另外,我认为您在这里不需要任何正则表达式,请使用explode
和strip_tags
:
$test = '<a id="test">One & -Two - Three</a>';
$res = explode(" - ", strip_tags($test));
echo $res[0]. "\n" . $res[1];
参见PHP演示
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句