我有一个各种格式的HTML,我试图用PHP标准化代码。在某些情况下,我会使用休息分隔线来接收它。我需要将这些标记改为$('a.lookupclick').attr('href','javascript:;');
标记。
1-首先,我尝试按<p>
拆分文本并将其添加到新持有者
<br>
结果:
<?php
$text = 'I am <br><br> <br><p> <br>your firnd <br></p><p> </p> ok?';
$breaks = array("<br />","<br>","<br/>","<p>","</p>");
$text = str_ireplace($breaks, "<br>", $text);
echo "R1: ".htmlspecialchars($text, ENT_QUOTES, "UTF-8");
$text = explode("<br>", $text);
$newText = null;
for($i=0; $i < count($text) ;$i++)
{
if(!empty($text[$i]))
$newText .= "<p>".$text[$i]."</p>";
}
$breaks = array("<p></p>","<p> </p>","<p> </p>");
echo "<br/>R2: ".htmlspecialchars($newText, ENT_QUOTES, "UTF-8");
$newText = str_ireplace($breaks, "", $newText);
echo "<br/>R3: ".htmlspecialchars($newText, ENT_QUOTES, "UTF-8");
?>
您是否建议我改进此代码? 我还有2个问题,那个......
如何避免在我的代码中生成额外的R1: I am <br><br> <br><br> <br>your firnd <br><br><br> <br> ok?
R2: <p>I am </p><p> </p><p> </p><p>your firnd </p><p> </p><p> ok?</p>
R3: <p>I am </p><p> </p><p>your firnd </p><p> </p><p> ok?</p>
,以及如何在p标记中删除p(id,class,meta等)的属性?
例如,对于此示例:
<p></p>
答案 0 :(得分:0)
试试这个: - 使用preg_replace()
和str_replace()
<?php
$text = 'I am <br><br><br><p id="ids" class="classes" meta-name="test"> <br>your firnd <br></p><p> </p> ok?';
$newcontent = preg_replace("/<b[^>]*?>/", "", $text);
echo $newcontent = str_replace("</b>", "<p />", $newcontent);