Open In App

PHP DOMXPath registerPhpFunctions() Function

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 illustrates the DOMXPath::registerPhpFunctions() function in PHP:

Program 1:

php
<?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", "http://php.net/xpath");

// 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 "\n" . $title;
}

?>

Output
Found 2 books starting with 'FOO':

FOO BAR
FOO BAZ

Program 2:

php
<?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", "http://php.net/xpath");

// 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 "\n" . $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

Next Article

Similar Reads