我有这样的功能:正则表达式匹配跨度或?
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
if(count($matches) > 0){
return $matches[1];
}
}
传递例如span
作为参数$tagname
让我来匹配任何span标签。我期望通过a|span
将允许我发动任何a
或span
标签。但它不匹配任何东西。为什么?
建议:使用['的XPath/DOM'(HTTP:/ /php.net/manual/en/class.domxpath.php) –
可能在preg_match之前preg_引用您的模式 – SomeDude
什么是失败的源代码示例?而不是'if(count($ matches)> 0){'只是'如果'preg_match'。 – chris85