在PHP中使用正则表达式XPath->评估 [英] Using Regex in PHP XPath->evaluate

查看:143
本文介绍了在PHP中使用正则表达式XPath->评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As a simple example I'm trying to get all the dom objects that contain email addresses.

    $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:函数将绑定到未定义的前缀fn ...

我也试过方法

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

没有成功。

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

I haven't been able to find many examples related to fn:match or php:function. Any assistance would be appreciated.

推荐答案

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

fn:matches is XPath 2.0, in DOMXPath, you have XPath 1.0. That's why it doesn't work, it's not supported.

手册中提供了 php:function 的示例 DOMXPath :: registerPhpFunction 。您需要具有PHP 5.3才能注册功能。

Examples for php:function are available on the manual page for DOMXPath::registerPhpFunctions. You need to have PHP 5.3 to register functions.

这篇关于在PHP中使用正则表达式XPath-&gt;评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆