DOMXPath::registerPhpFunctions Register PHP functions as XPath functions &reftitle.description; public voidDOMXPath::registerPhpFunctions stringarraynullrestrict&null; This method enables the ability to use PHP functions within XPath expressions. &reftitle.parameters; restrict Use this parameter to only allow certain functions to be called from XPath. This parameter can be one of the following: a string (a function name), an indexed array of function names, or an associative array with keys being the function name and the associated value being the callable. &reftitle.returnvalues; &return.void; &reftitle.errors; Throws a ValueError if a callback name is not valid. &dom.errors.compliant.common; Throws a TypeError if a given callback is not callable. &reftitle.changelog; &Version; &Description; 8.4.0 Invalid callback names now throws a ValueError. Passing a non-callable entry now throws a TypeError. 8.4.0 It is now possible to use callables for callbacks when using restrict with array entries. &reftitle.examples; The following examples use book.xml which contains the following: book.xml PHP Basics Jim Smith Jane Smith PHP Secrets Jenny Smythe XML basics Joe Black ]]> <methodname>DOMXPath::registerPhpFunctions</methodname> with <literal>php:functionString</literal> load('examples/book-simple.xml'); $xpath = new DOMXPath($doc); // Register the php: namespace (required) $xpath->registerNamespace("php", "http://php.net/xpath"); // Register PHP functions (no restrictions) $xpath->registerPhpFunctions(); // Call substr function on the book title $nodes = $xpath->query('//book[php:functionString("substr", title, 0, 3) = "PHP"]'); echo "Found {$nodes->length} books starting with 'PHP':\n"; foreach ($nodes as $node) { $title = $node->getElementsByTagName("title")->item(0)->nodeValue; $author = $node->getElementsByTagName("author")->item(0)->nodeValue; echo "$title by $author\n"; } ?> ]]> &example.outputs.similar; <methodname>DOMXPath::registerPhpFunctions</methodname> with <literal>php:function</literal> load('examples/book-simple.xml'); $xpath = new DOMXPath($doc); // Register the php: namespace (required) $xpath->registerNamespace("php", "http://php.net/xpath"); // Register PHP functions (has_multiple only) $xpath->registerPhpFunctions("has_multiple"); function has_multiple($nodes) { // Return true if more than one author return count($nodes) > 1; } // Filter books with multiple authors $books = $xpath->query('//book[php:function("has_multiple", author)]'); echo "Books with multiple authors:\n"; foreach ($books as $book) { echo $book->getElementsByTagName("title")->item(0)->nodeValue . "\n"; } ?> ]]> &example.outputs.similar; <methodname>DOMXPath::registerPhpFunctions</methodname> with a <type>callable</type> load('examples/book-simple.xml'); $xpath = new DOMXPath($doc); // Register the php: namespace (required) $xpath->registerNamespace("php", "http://php.net/xpath"); // Register PHP functions (has_multiple only) $xpath->registerPhpFunctions(["has_multiple" => fn ($nodes) => count($nodes) > 1]); // Filter books with multiple authors $books = $xpath->query('//book[php:function("has_multiple", author)]'); echo "Books with multiple authors:\n"; foreach ($books as $book) { echo $book->getElementsByTagName("title")->item(0)->nodeValue . "\n"; } ?> ]]> &example.outputs.similar; &reftitle.seealso; DOMXPath::registerNamespace DOMXPath::query DOMXPath::evaluate XSLTProcessor::registerPHPFunctions