我有这个 HTML 内容:
$test = '<section><div class="row">
<div class="col-xs-12" data-type="container-content">
<section data-type="component-photo"><div class="photo-panel">
<img src="http://somewhere.com/XpV8lCLD6pChrysanthemum.jpg" width="100%" height="" style="display: inline-block;">
</div></section></div>
</div></section>';
我有 str_replace 的这个代码,以便它为img添加一个结束标记:
$new = str_replace('"></div>', '"/></div>', $test);
echo $new;
使用检查元素检查时,图像未按预期关闭。我在这里做错了什么?
补充笔记:
我需要 PHPWord 的结束标记才能工作。此外, 的值$test
来自,$_POST
因为这是通过 AJAX 提供的。
谢谢
标签和结束标签之间有一个换行符。您应该重新格式化 $test 变量或使用:
$new = preg_replace('/>[\n\r]<\/div>/', '/></div>', $test);
然而,您在这里处于危险境地,因为所有比赛都将被替换。HTML 是一种不能很好地与 regex 配合使用的语言。
如果可能的话,最好在它采用 html 形式之前解决问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句