enl*_*loz 8 php regex html-parsing
任务是<img>用<div>标签和src属性替换给定字符串中的所有标签作为内部文本.在寻找答案时,我发现了类似的问题
<?php
$content = "this is something with an <img src=\"test.png\"/> in it.";
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
?>
Run Code Online (Sandbox Code Playgroud)
结果:
this is something with an (image) in it.
Run Code Online (Sandbox Code Playgroud)
问题:如何升级脚本蚂蚁得到这个结果:
this is something with an <div>test.png</div> in it.
Run Code Online (Sandbox Code Playgroud)
FtD*_*Xw6 11
这是PHP DOMDocument类擅长的问题:
$dom = new DOMDocument();
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('img') as $img) {
// put your replacement code here
}
$content = $dom->saveHTML();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10382 次 |
| 最近记录: |