will Farrell
will Farrell

Reputation: 1759

Using Regex in PHP XPath->evaluate

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";
    }

I'm getting the error: "DOMXPath::evaluate() [domxpath.evaluate]: xmlXPathCompOpEval: function matches bound to undefined prefix fn ..."

I've also tried the approach

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

without success.

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

Upvotes: 0

Views: 1418

Answers (1)

hakre
hakre

Reputation: 197842

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

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

Upvotes: 5

Related Questions