【发布时间】:2012-11-26 20:26:24
【问题描述】:
任务是用<div>标签和src属性替换给定字符串中的所有<img>标签作为内部文本。
在寻找答案时我找到了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.
问题:如何升级脚本ant得到这个结果:
this is something with an <div>test.png</div> in it.
【问题讨论】:
-
您不想使用正则表达式来解析 HTML。他们不能胜任这项任务。您的正则表达式解决方案非常脆弱。 htmlparsing.com/regexes.html 解释了原因。
标签: php regex html-parsing