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.

hakre
  • 193,403
  • 52
  • 435
  • 836
will Farrell
  • 1,733
  • 1
  • 16
  • 21
  • can I suggest trying it with double quotes within the matches() function? – Scuzzy Nov 07 '11 at 00:07
  • 1
    Have you read the [`DOMXPath::registerPhpFunctions()` page](http://www.php.net/manual/en/domxpath.registerphpfunctions.php)? – Phil Nov 07 '11 at 00:15

1 Answers1

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.

hakre
  • 193,403
  • 52
  • 435
  • 836