php - 如何使用 PHP 获取以“abcd”开头的 html 标签?
问题描述
我有以下html代码:
<div class="pictures">
<figure>
<img src="img/foo.jpg" height="400" width="400" id="abcd001"/>
<figcaption>foo</figcaption>
</figure>
<figure>
<img src="img/bar.jpg" height="400" width="400" id="abcd002"/>
<figcaption>bar</figcaption>
</figure>
<figure>
<img src="img/Joe.jpg" height="400" width="400" id="abcd003"/>
<figcaption>Joe</figcaption>
</figure>
</div>
<div id="abcd004">
Lorem Ipsum.
<div>
并试图获取所有 html 标记及其 id 以“abcd”开头的孩子。XDOMDocument 类的查询功能似乎不适用于正则表达式。
$dom = new DomDocument();
$dom->load("/var/www/html/myWebsite.html")
$xpath = new DOMXPath($dom);
$xdom = $xpath->query("//img[@id='abcd*']");
foreach($xdom as $entry)
{
echo $entry;
}
有什么建议么?
编辑: start-with 不起作用,因为该功能似乎不适用于 html 标签的 ID
解决方案
我认为您没有使用正确的语法start-with
,请尝试以下操作:
$xpath = new DOMXPath($dom);
$xdom = $xpath->query("//img[starts-with(@id, 'abcd')]");
foreach($xdom as $entry) {
echo $entry->getAttribute('src') . "\n";
}
在这里看到它的工作:https ://3v4l.org/04i5a
推荐阅读
- facebook - Facebook Graph API - 无法访问见解
- php - 在 Laravel 中显示从 json 检索到的数据
- sql - SQL使用两张表创建二元相关表
- printing - Ghostscript 打印驱动程序强制 cmyk 而不是 rgb?
- python - 这个 python wrapper() 函数是如何工作的?
- git - 使用 GitHub 用户推送到 Github,而不是添加一些名称
- regression - R中的混合数据采样(midas_r)
- typescript - Npm install 在 React-Native 中给出错误
- postgresql - 如何杀死他们的进程不再存在的查询
- python - Python拆分第一个“中间”和最后一个单词