0

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.

2
  • can I suggest trying it with double quotes within the matches() function? Commented Nov 7, 2011 at 0:07
  • 1
    Have you read the DOMXPath::registerPhpFunctions() page? Commented Nov 7, 2011 at 0:15

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.