PHP – 替换标签并返回src

前端之家收集整理的这篇文章主要介绍了PHP – 替换标签并返回src前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使命是取代所有< img>带有< div>的给定字符串中的标签标签和src属性作为内部文本.
在寻找答案时我找到了 similar question
<?PHP

    $content = "this is something with an <img src=\"test.png\"/> in it.";
    $content = preg_replace("/<img[^>]+\>/i","(image) ",$content); 
    echo $content;

?>

结果:

this is something with an (image)  in it.

问题:如何升级脚本蚂蚁得到这个结果:

this is something with an <div>test.png</div>  in it.
这是PHPDOMDocument类擅长的问题:
$dom = new DOMDocument();
$dom->loadHTML($content);

foreach ($dom->getElementsByTagName('img') as $img) {
    // put your replacement code here
}

$content = $dom->saveHTML();
原文链接:https://www.f2er.com/php/130612.html

猜你在找的PHP相关文章