任务是替换所有<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.
问题:如何升级script ant得到这个结果:
this is something with an <div>test.png</div> in it.
最佳答案
这是 PHP 的 DOMDocument
的问题类(class)擅长:
$dom = new DOMDocument();
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('img') as $img) {
// put your replacement code here
}
$content = $dom->saveHTML();
关于PHP - 替换 <img> 标签并返回 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783760/