加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP – 替换标签并返回src

发布时间:2020-12-13 14:13:40 所属栏目:PHP教程 来源:网络整理
导读:使命是取代所有 img带有 div的给定字符串中的标签标签和src属性作为内部文本. 在寻找答案时我找到了 similar question ?php $content = "this is something with an img src="test.png"/ in it."; $content = preg_replace("/img[^]+/i","(image) ",$cont
使命是取代所有< 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.
这是PHP的 DOMDocument类擅长的问题:
$dom = new DOMDocument();
$dom->loadHTML($content);

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

$content = $dom->saveHTML();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读