使命是取代所有< img>带有< div>的给定字符串中的标签标签和src属性作为内部文本.
在寻找答案时我找到了 similar question
在寻找答案时我找到了 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.
这是PHP的
DOMDocument
类擅长的问题:
$dom = new DOMDocument(); $dom->loadHTML($content); foreach ($dom->getElementsByTagName('img') as $img) { // put your replacement code here } $content = $dom->saveHTML();