Php 如何从div中提取图像URL?
我想用PHP从div中提取背景图像url。我想在字符串中搜索类并提取背景图像url 例如:Php 如何从div中提取图像URL?,php,css,url,string-parsing,Php,Css,Url,String Parsing,我想用PHP从div中提取背景图像url。我想在字符串中搜索类并提取背景图像url 例如: <div class="single-post-image" style="background-image: url(https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg)"></div> 这应该起作用: preg_match_all('/background-i
<div class="single-post-image" style="background-image: url(https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg)"></div>
这应该起作用:
preg_match_all('/background-image: url\((.*?)\)/', $your_html, $output, PREG_SET_ORDER);
更改最后一个参数,以便以您偏好的形式(数组)获得输出:您可以使用直接的正则表达式,但就我个人而言,我将使用dom文档/xpath从后面的元素中删除,然后正则表达式从样式中删除值
<?php
$html = '
<html><head></head><body>
<div class="single-post-image" style="background-image:url(https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg)"></div>
<div class="single-post-image" style="background-image: url(https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg )"></div>
<div class="single-post-image" style="background-image: url( https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg)"></div>
<div class="single-post-image" style="background-image: url(\'https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg\')"></div>
<div class="single-post-image"></div>
</body>
</html>';
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DomXPath($dom);
$images = [];
foreach ($xpath->query("//*[contains(@class, 'single-post-image')]") as $img) {
if ($img->hasAttribute('style')) {
preg_match('/url\((.*)\)/', $img->getAttribute('style'), $match);
if (isset($match[1])) $images[] = trim($match[1], '\'" ');
}
}
print_r($images);
例如:
代码稍微多一点,但我相信它比在一个巨大的HTML文档上重新登录更健壮、更高效
<?php
$html = '
<html><head></head><body>
<div class="single-post-image" style="background-image:url(https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg)"></div>
<div class="single-post-image" style="background-image: url(https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg )"></div>
<div class="single-post-image" style="background-image: url( https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg)"></div>
<div class="single-post-image" style="background-image: url(\'https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg\')"></div>
<div class="single-post-image"></div>
</body>
</html>';
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DomXPath($dom);
$images = [];
foreach ($xpath->query("//*[contains(@class, 'single-post-image')]") as $img) {
if ($img->hasAttribute('style')) {
preg_match('/url\((.*)\)/', $img->getAttribute('style'), $match);
if (isset($match[1])) $images[] = trim($match[1], '\'" ');
}
}
print_r($images);
Array
(
[0] => https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg
[1] => https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg
[2] => https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg
[3] => https://www.mmowg.net/wp-content/uploads/2020/11/a8Tnv1kVyXY.jpg
)