PHP | DOMXPath registerPhpFunctions() Function

The DOMXPath::registerPhpFunctions() function is an inbuilt function in PHP which is used to enable the ability to use PHP functions within XPath expressions.
Syntax:
void DOMXPath::registerPhpFunctions( mixed $restrict )
Parameters: This function accepts an optional single parameter $restrict which holds the function to restrict.
Return Value: This function does not return any value.
Below given programs illustrate the DOMXPath::registerPhpFunctions() function in PHP:
Program 1:
<?php   // Create a new DOMDocument instance $document = new DOMDocument();   // Create a XML $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <books>     <book>         <title>FOO BAR</title>         <author>Mr. A</author>         <author>Mr. B</author>     </book>     <book>         <title>FOO BAZ</title>         <author>Mr. C</author>     </book> </books> XML;   // Load the XML $document->loadXML($xml);   // Create a new DOMXPath instance $xpath = new DOMXPath($document);   // Register the php: namespace $xpath->registerNamespace("php",   // Register PHP functions $xpath->registerPHPFunctions();   // Use the PHP function to find // the books starting with FOO $query = '//book[php:functionString('        . '"substr", title, 0, 3) = "FOO"]';   // Execute the query $entries = $xpath->evaluate($query);   echo "Found $entries->length books"        . " starting with 'FOO':\n"; foreach ($entries as $node) {     $title = $node->getElementsByTagName(                 "title")->item(0)->nodeValue;     echo "<br>$title"; } ?> |
Output:
Found 2 books starting with 'FOO': FOO BAR FOO BAZ
Program 2:
<?php   // Create a new DOMDocument instance $document = new DOMDocument();    // Create a XML $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <books>     <book>         <title>FOO BAR</title>         <author>Mr. A</author>         <author>Mr. B</author>     </book>     <book>         <title>FOO BAZ</title>         <author>Mr. C</author>     </book> </books> XML;    // Load the XML $document->loadXML($xml);    // Create a new DOMXPath instance $xpath = new DOMXPath($document);    // Register the php: namespace $xpath->registerNamespace("php",    // Register PHP functions $xpath->registerPHPFunctions();    // Use the manually created // PHP function in query $query = '//book[php:function('        . '"has_multiple", author)]';    // Execute the query $entries = $xpath->evaluate($query);    echo "Found $entries->length books "        . "with multiple authors:\n";   foreach ($entries as $node) {     $title = $node->getElementsByTagName(             "title")->item(0)->nodeValue;     echo "<br>$title"; }    function has_multiple($nodes) {           // Return true if more than     // one author is there     return count($nodes) > 1; } ?> |
Output:
Found 1 books with multiple authors: FOO BAR
Reference: https://www.php.net/manual/en/domxpath.registerphpfunctions.php
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



