是否可以找到所有未包装在链接中的图像?
内容示例:
$content = 'Something <a href="/blog">Blog</a>, lorem ipsum
<img class="wp-image-1234" src="http://www.website.com/img/image.jpg">
dolor sit amet
<a href="/images"><img class="2344" src="http://www.website.com/img/image.jpg"> Images</a> ipsum dolor lorem
<img class="aligncenter size-medium wp-image-3201" src="http://www.website.com/wp-content/uploads/2018/07/image-18-300x200.jpg" alt="" srcset="http://www.website.com/wp-content/uploads/2018/07/image-18-300x200.jpg 300w, http://www.website.com/wp-content/uploads/2018/07/image-18-768x512.jpg 768w, http://www.website.com/wp-content/uploads/2018/07/image-18-128x85.jpg 128w, http://www.website.com/wp-content/uploads/2018/07/image-18-375x250.jpg 375w, http://www.website.com/wp-content/uploads/2018/07/image-18.jpg 800w" sizes="(max-width: 300px) 100vw, 300px">
<a href="/image">
<img class="aligncenter size-medium wp-image-3200" src="http://www.website.com/wp-content/uploads/2018/07/image-18-300x200.jpg" alt="" srcset="http://www.website.com/wp-content/uploads/2018/07/image-18-300x200.jpg 300w, http://www.website.com/wp-content/uploads/2018/07/image-18-768x512.jpg 768w, http://www.website.com/wp-content/uploads/2018/07/image-18-128x85.jpg 128w, http://www.website.com/wp-content/uploads/2018/07/image-18-375x250.jpg 375w, http://www.website.com/wp-content/uploads/2018/07/image-18.jpg 800w" sizes="(max-width: 300px) 100vw, 300px">
</a>';
我的意图是根据要从图像类名称“ wp-image-xxxx”(Wordpress)中提取的ID手动为每个图像包装一个新链接。
PHP语言,这是模板https://regexr.com/3s0jk
答案 0 :(得分:0)
使用DOMDocument / DOMXPath:
$doc = DOMDocument::loadHtml($content);
$selector = new DOMXPath($doc);
$query = '//img[not(parent::a)]';
foreach($selector->query($query) as $node) {
echo $node->getAttribute('src'), PHP_EOL;
}