(PHP 8 >= 8.4.0)
DOMXPath::quote — 为XPath表达式引用字符串
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')