大网数据广告活动
PHP 语言与运行手册

DOMXPath::registerPhpFunctions · Register PHP functions as XPath functions

· 52CE 我爱测

DOMXPath::registerPhpFunctions — Register PHP functions as XPath functions

DOMXPath::registerPhpFunctions

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

DOMXPath::registerPhpFunctions — Register PHP functions as XPath functions

说明

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

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

参数

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.

返回值

没有返回值。

错误/异常

  • Throws a ValueError if a callback name is not valid.

  • Throws a ValueError if options contains an invalid option.

  • Throws a ValueError if overrideEncoding is an unknown encoding.

  • Throws a TypeError if a given callback is not callable.

更新日志

版本 说明
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.

示例

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

示例 #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>

示例 #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";
}

?>

以上示例的输出类似于:

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

示例 #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";
}

?>

以上示例的输出类似于:

Books with multiple authors:
PHP Basics

示例 #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";
}

?>

以上示例的输出类似于:

Books with multiple authors:
PHP Basics

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。