【问题标题】:Using Regex in PHP XPath->evaluate在 PHP XPath 中使用正则表达式->评估
【发布时间】:2011-12-23 07:13:51
【问题描述】:

作为一个简单的例子,我试图获取所有包含电子邮件地址的 dom 对象。

    $doc = new DOMDocument;
    $doc->loadHTML($page);

    $xpath = new DOMXPath($doc);
    $body = $doc->getElementsByTagName('table')->item(0);
    $query = "//text()[fn:matches(., '[\w\d\.-]+@[\w\d\.-]+\.[a-zA-Z]{2,4}')]"; // 
    $entries = $xpath->evaluate($query, $body); // 
    foreach ($entries as $entry) {
        echo "Found {$entry->textContent}<br>\n";
    }

我收到错误消息:"DOMXPath::evaluate() [domxpath.evaluate]: xmlXPathCompOpEval: function matches bound to undefined prefix fn ..."

我也试过这个方法

//text()[
    php:function('BooleanPregMatch', '[\w\d\.-]+@[\w\d\.-]+\.[a-zA-Z]{2,4}', .)
]

没有成功。

我找不到很多与 fn:match 或 php:function 相关的示例。任何帮助将不胜感激。

【问题讨论】:

标签: php regex dom xpath


【解决方案1】:

fn:matches 是 XPath 2.0,在 DOMXPath 中,你有 XPath 1.0。这就是为什么它不起作用,不受支持。

php:function 的示例可在DOMXPath::registerPhpFunctions 的手册页上找到。您需要有 PHP 5.3 才能注册函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多