任务是将给定字符串中的所有<img>
标记替换为<div>
标记和src
属性作为内部文本。 为了寻找答案,我发现similar questionPHP - 替换<img>标记并返回src
<?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.
你不想使用正则表达式来解析HTML。他们不能胜任这项任务。您的正则表达式解决方案非常脆弱。 http://htmlparsing.com/regexes.html解释了原因。 –