这是一个字符串:
$test = '<a id="test">One & -Two - Three</a>';
我想将这两个字符串捕获到两个不同的变量中,比如
$string1 = 'One & -Two';
和
$string2 = 'Three';
所以我使用preg_match_all,但我有一个问题与regexp:
preg_match_all('#([-;'w' '.'/'''d'(')'&]+)+ - (['w+' '.'-]+)+#', $test, $matches);
谁能给我解释一下为什么它不起作用?
)
(([-;'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演示