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

DOMXPath::quote · Quotes a string for use in an XPath expression

· 52CE 我爱测

DOMXPath::quote — Quotes a string for use in an XPath expression

DOMXPath::quote

(PHP 8 >= 8.4.0)

DOMXPath::quote — Quotes a string for use in an XPath expression

Description

public static function DOMXPath::quote(string $str): string

Quotes str for use in an XPath expression.

Parameters

str

The string to quote.

Return Values

Returns a quoted string to be used in an XPath expression.

Examples

Example #1 Matching attribute value with quotes

<?php
$doc = new DOMDocument;
$doc->loadXML(<<<XML
<books>
    <book name="'quoted' name">Book title</book>
</books>
XML);

$xpath = new DOMXPath($doc);

$query = "//book[@name=" . DOMXPath::quote("'quoted' name") . "]";
echo $query, "\n";

$entries = $xpath->query($query);

foreach ($entries as $entry) {
    echo "Found ", $entry->textContent, "\n";
}
?>

The above example will output:

//book[@name="'quoted' name"]
Found Book title

Mixed quote types are also supported:

<?php
echo DOMXPath::quote("'different' \"quote\" styles");
?>

The above example will output:

concat("'different' ",'"quote" styles')

See Also

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

资料来源与适用范围

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

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

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