str_ireplace with replace结束了我的html标签。
$txt = '<p><strong>Add on </strong>[number],<strong> number nr </strong></p>';
$htmlValue = '<div class="resizing-input"><input type="text" value="1"><span style="display:none"></span></div>';
$text = str_ireplace('[number]', $htmlValue, $txt);//It needs to be case-insensitive so str_ireplace not str_replace.
echo'<pre>';print_r($text);echo'</pre>';die;
//return: <pre><p><strong>Add on </strong></p><div class="resizing-input"><input type="text" value="1"><span style="display:none"></span></div>,<strong> number nr </strong><p></p></pre>
//should return: <pre><p><strong>Add on </strong><div class="resizing-input"><input type="text" value="1"><span style="display:none"></span></div>,<strong> number nr </strong></p></pre>
为什么会这样?
我想在[]和html之间替换字符串,但它会结束另一个html </p>
标记。 str_replace也是如此。
答案 0 :(得分:0)
问题是我无法在<div>
元素中写<p>
标记
p元素的语法不允许div子节点,并且可以省略结束标记</p>
,验证程序(和浏览器)在解析时遇到</p>
标记时隐含<div>
ap元素。也就是说,当正在解析p(或“打开”)时,div元素的开始标记会隐式关闭它。