# DOMXPath::registerPhpFunctions

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DOMXPath::registerPhpFunctions — Register PHP functions as XPath functions

### Beschreibung

public DOMXPath::registerPhpFunctions(string|array|null `$restrict` = `null`): void

This method enables the ability to use PHP functions within XPath expressions.

### Parameter-Liste

`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.

### Rückgabewerte

Es wird kein Wert zurückgegeben.

### Fehler/Exceptions

- Throws a ValueError if a callback name is not valid.
- Wenn `options` eine ungültige Option enthält, wird ein ValueError ausgelöst.
- Wenn `overrideEncoding` eine unbekannte Kodierung ist, wird ein ValueError ausgelöst.
- Throws a TypeError if a given callback is not callable.

### Changelog

Version | Beschreibung
--- | ---
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.

### Beispiele

The following examples use book.xml which contains the following:

Beispiel #1 book.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <books>
     <book>
      <title>PHP Basics</title>
      <author>Jim Smith</author>
      <author>Jane Smith</author>
     </book>
     <book>
      <title>PHP Secrets</title>
      <author>Jenny Smythe</author>
     </book>
     <book>
      <title>XML basics</title>
      <author>Joe Black</author>
     </book>
    </books>

Beispiel #2 DOMXPath::registerPhpFunctions() with `php:functionString`

`<?php
$doc = new DOMDocument;
$doc->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";
}
?>`

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

    Found 2 books starting with 'PHP':
    PHP Basics by Jim Smith
    PHP Secrets by Jenny Smythe

Beispiel #3 DOMXPath::registerPhpFunctions() with `php:function`

`<?php
$doc = new DOMDocument;
$doc->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";
}
?>`

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

    Books with multiple authors:
    PHP Basics

Beispiel #4 DOMXPath::registerPhpFunctions() with a callable

`<?php
$doc = new DOMDocument;
$doc->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";
}
?>`

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

    Books with multiple authors:
    PHP Basics

### Siehe auch

- DOMXPath::registerNamespace() - Registers the namespace with the DOMXPath object
- DOMXPath::query() - Evaluates the given XPath expression
- DOMXPath::evaluate() - Evaluates the given XPath expression and returns a typed result if possible
- XSLTProcessor::registerPHPFunctions() - Aktiviert die Nutzbarkeit von PHP-Funktionen als XSLT-Funktionen

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open