preg_replace P标签内的目标图片
preg_replace target images within P tags
我正在使用 preg_replace 更改一些内容,我有 2 种不同类型的图像...
<p>
<img class="responsive" src="image.jpg">
</p>
<div class="caption">
<img class="responsive" src="image2.jpg">
</div>
我正在像这样使用 preg_replace 在图像周围添加一个容器 div...
function filter_content($content)
{
$pattern = '/(<img[^>]*class=\"([^>]*?)\"[^>]*>)/i';
$replacement = '<div class="inner "></div>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
有没有办法修改它只影响P标签中的图片?反之亦然,所以我也可以在标题 div?
中定位图像
绝对。
$dom = new DOMDocument();
$dom->loadHTML("<body><!-- DOMSTART -->".$content."<!-- DOMEND --></body>");
$xpath = new DOMXPath($dom);
$images = $xpath->query("//p/img");
foreach($images as $img) {
$wrap = $dom->createElement("div");
$wrap->setAttribute("class","inner ".$img->getAttribute("class"));
$img->parentNode->replaceChild($wrap,$img);
$wrap->appendChild($img);
}
$out = $dom->saveHTML();
preg_match("/<!-- DOMSTART -->(.*)<!-- DOMEND -->/s",$out,$match);
return $match[1];
值得注意的是,虽然使用正则表达式解析任意 HTML 是一场等待发生的灾难,但使用带有标记的解析器然后根据这些标记进行匹配是绝对安全的。
根据需要调整 XPath 查询and/or 内部操作。
使用 html
解析器而不是正则表达式,例如 DOMDocument
,即:
$html = <<< EOF
<p>
<img class="responsive" src="image.jpg">
</p>
<div class="caption">
<img class="responsive" src="image2.jpg">
</div>
EOF;
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$images = $xpath->query('//p/img[contains(@class,"responsive")]');
$new_src_url = "some_image_name.jpg";
foreach($images as $image)
{
$image->setAttribute('src', $new_src_url);
$dom->saveHTML($tag);
}
我正在使用 preg_replace 更改一些内容,我有 2 种不同类型的图像...
<p>
<img class="responsive" src="image.jpg">
</p>
<div class="caption">
<img class="responsive" src="image2.jpg">
</div>
我正在像这样使用 preg_replace 在图像周围添加一个容器 div...
function filter_content($content)
{
$pattern = '/(<img[^>]*class=\"([^>]*?)\"[^>]*>)/i';
$replacement = '<div class="inner "></div>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
有没有办法修改它只影响P标签中的图片?反之亦然,所以我也可以在标题 div?
中定位图像绝对。
$dom = new DOMDocument();
$dom->loadHTML("<body><!-- DOMSTART -->".$content."<!-- DOMEND --></body>");
$xpath = new DOMXPath($dom);
$images = $xpath->query("//p/img");
foreach($images as $img) {
$wrap = $dom->createElement("div");
$wrap->setAttribute("class","inner ".$img->getAttribute("class"));
$img->parentNode->replaceChild($wrap,$img);
$wrap->appendChild($img);
}
$out = $dom->saveHTML();
preg_match("/<!-- DOMSTART -->(.*)<!-- DOMEND -->/s",$out,$match);
return $match[1];
值得注意的是,虽然使用正则表达式解析任意 HTML 是一场等待发生的灾难,但使用带有标记的解析器然后根据这些标记进行匹配是绝对安全的。
根据需要调整 XPath 查询and/or 内部操作。
使用 html
解析器而不是正则表达式,例如 DOMDocument
,即:
$html = <<< EOF
<p>
<img class="responsive" src="image.jpg">
</p>
<div class="caption">
<img class="responsive" src="image2.jpg">
</div>
EOF;
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$images = $xpath->query('//p/img[contains(@class,"responsive")]');
$new_src_url = "some_image_name.jpg";
foreach($images as $image)
{
$image->setAttribute('src', $new_src_url);
$dom->saveHTML($tag);
}