Php preg_替换P标记中的目标图像

Php preg_替换P标记中的目标图像,php,preg-replace,Php,Preg Replace,我使用preg_替换来更改一些内容,我有两种不同类型的图像 <p> <img class="responsive" src="image.jpg"> </p> <div class="caption"> <img class="responsive" src="image2.jpg"> </div> function filter_content($content) { $patt

我使用preg_替换来更改一些内容,我有两种不同类型的图像

<p>
    <img class="responsive" src="image.jpg">
</p>

<div class="caption">
    <img class="responsive" src="image2.jpg">
</div>
function filter_content($content)
    {
        $pattern = '/(<img[^>]*class=\"([^>]*?)\"[^>]*>)/i';
        $replacement = '<div class="inner $2">$1</div>';
        $content = preg_replace($pattern, $replacement, $content);
        return $content;
    }

我使用preg_replace像这样在图像周围添加一个容器div

<p>
    <img class="responsive" src="image.jpg">
</p>

<div class="caption">
    <img class="responsive" src="image2.jpg">
</div>
function filter_content($content)
    {
        $pattern = '/(<img[^>]*class=\"([^>]*?)\"[^>]*>)/i';
        $replacement = '<div class="inner $2">$1</div>';
        $content = preg_replace($pattern, $replacement, $content);
        return $content;
    }
函数过滤器\u内容($content)
{
$pattern='/(]*class=\”([^>]*?)\“[^>]*>)/i';
$replacement='$1';
$content=preg_replace($pattern,$replacement,$content);
返回$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];
$dom=newdomdocument();
$dom->loadHTML(“$content.”);
$xpath=newdomxpath($dom);
$images=$xpath->query(“//p/img”);
foreach($img形式的图像){
$wrap=$dom->createElement(“div”);
$wrap->setAttribute(“类”,“内部”。$img->getAttribute(“类”);
$img->parentNode->replaceChild($wrap,$img);
$wrap->appendChild($img);
}
$out=$dom->saveHTML();
预匹配(“/(.*)/s”,$out,$match);
返回$match[1];
值得注意的是,虽然使用正则表达式解析任意HTML是一场即将发生的灾难,但使用带有标记的解析器,然后根据这些标记进行匹配是完全安全的

根据需要调整XPath查询和/或内部操作。

绝对正确

$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];
$dom=newdomdocument();
$dom->loadHTML(“$content.”);
$xpath=newdomxpath($dom);
$images=$xpath->query(“//p/img”);
foreach($img形式的图像){
$wrap=$dom->createElement(“div”);
$wrap->setAttribute(“类”,“内部”。$img->getAttribute(“类”);
$img->parentNode->replaceChild($wrap,$img);
$wrap->appendChild($img);
}
$out=$dom->saveHTML();
预匹配(“/(.*)/s”,$out,$match);
返回$match[1];
值得注意的是,虽然使用正则表达式解析任意HTML是一场即将发生的灾难,但使用带有标记的解析器,然后根据这些标记进行匹配是完全安全的


根据需要调整XPath查询和/或内部操作。

使用
html
解析器而不是regex,
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);
}
$html=query('//p/img[contains(@class,“responsive”)]);
$new\u src\u url=“some\u image\u name.jpg”;
foreach($images作为$image)
{
$image->setAttribute('src',$new\u src\u url);
$dom->saveHTML($tag);
}

使用
html
解析器而不是regex,
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);
}
$html=query('//p/img[contains(@class,“responsive”)]);
$new\u src\u url=“some\u image\u name.jpg”;
foreach($images作为$image)
{
$image->setAttribute('src',$new\u src\u url);
$dom->saveHTML($tag);
}

混合使用DOMDocument和xpath将完成作业混合使用DOMDocument和xpath将完成作业使用此方法优于regex有什么好处?使用此方法优于regex有什么好处?