PHP Conference Japan 2024

DOMXPath::quote

(PHP 8 >= 8.4.0)

DOMXPath::quote 为XPath表达式引用字符串

描述

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

为在XPath表达式中使用而引用str

参数

str
要引用的字符串。

返回值

返回要在XPath表达式中使用的已引用字符串。

示例

示例 #1 匹配带引号的属性值

<?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";
}
?>

以上示例将输出

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

也支持混合引号类型

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

以上示例将输出

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

参见

添加注释

用户贡献的注释

此页面没有用户贡献的注释。
To Top