【发布时间】:2021-05-23 18:30:32
【问题描述】:
我有以下 $html 内容的场景。我想检查 html 内容是否以没有任何文本内容的媒体(图像或视频、iframe)开头,如第三种情况。
//第一个p标签和图片标签之间没有内容
$html = '<p dir="ltr"><img src="imageurl" class="img"><br></p>
<div>some content </div>';
//第一个p标签和video标签之间没有内容
$html = '<p dir="ltr"><video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video></p>
<div>some content </div>';
//在第一个 p 标签内有内容
$html = '<p dir="ltr">here we have text<img src="imageurl" class="img"><br></p>
<div>some content </div>';
我试过这样但没有运气,请指教
$dom = new DOMDocument();
$dom->loadHTML($html);//that's HTML of my document, string
$xpath = new DOMXPath($dom);
$xpath_resultset = $xpath->query("p/following-sibling::node()[not(preceding-sibling::img) and not(self::img)]");
【问题讨论】:
-
那么这三种场景你想接受哪一种呢?
-
我的问题中提到的 @endeavour first 2 html 输出是真实场景,第三个 html 输出在第一个
标签与
之间有内容