xpath()
函数用于在 XML 文档中执行 XPath 查询。
语法:
xpath($xml, $query)
参数:
$xml
:必需,指定要查询的 XML 文档。$query
:必需,指定要执行的 XPath 查询。返回值: 返回一个包含查询结果的数组。
示例:
$xml = <<<XML
<books>
<book>
<title>PHP Cookbook</title>
<author>David Sklar, Adam Trachtenberg</author>
<year>2014</year>
</book>
<book>
<title>JavaScript: The Good Parts</title>
<author>Douglas Crockford</author>
<year>2008</year>
</book>
</books>
XML;
$result = xpath($xml, "//book/title");
print_r($result);
输出结果:
Array
(
[0] => SimpleXMLElement Object
(
[0] => PHP Cookbook
)
[1] => SimpleXMLElement Object
(
[0] => JavaScript: The Good Parts
)
)
在上面的示例中,通过 XPath 查询 //book/title
,返回了所有 <book>
元素下的 <title>
元素的内容。
上一篇:php怎么把变量转为字符串
下一篇:php怎么将字符转化为数字类型
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站