php去掉<div id="content" style=" width:100px"></div>中的内容,就保留<div></div>

不是单指在<div>里,包括<li>这些标签里的id或者class 只要是标签里面的演示或类都要去掉的那种方法。能否举一个完整的例子。我好实验一下,谢谢!
最新回答
冷魅

2024-09-10 07:47:38

<?php

$str = '<div id="content" style=" width:100px">contents</div>';

$output = preg_replace('/<div[^>]*>/i' , '<div>' , $str);

echo htmlspecialchars($output);

?>
//============================================================
<?php

$ary_html = array(
'div' => '<div id="content" style=" width:100px"></div>'
,'span' => '<span id="s" style=" width:100px"></span>'
,'td' => '<td id="sd" style=" width:100px"></td>'
,'tr' => '<tr id="aaa" style=" width:100px"></tr>'
,'th' => '<th id="cc" style=" width:100px"></th>'
,'body' => '<body id="ddd" style=" width:100px"></body>'
,'body' => '<body id="dafa" style=" width:100px"></body>'
,'html' => '<html xmlns="
http://www.w3.org/1999/xhtml
"></html>'
,'head' => '<head id="head"></head>'
,'title' => '<title al=\'asd\'>this is the content</title>'

);

$reg = '/(<\w+)[^<]+>/im';

foreach($ary_html as $key => $val){

$str = preg_replace($reg , "$1>" , $val);

echo "<li>$key : " , color( htmlspecialchars($val) , 'red') , ' => ' , color(htmlspecialchars($str) , 'green');

}

function color($html , $color){

return "<font color=$color>" . $html . '</font>';
}

?>
只影

2024-09-10 03:33:00

用正则表达式

或者

用替换函数将
<div id="content" style=" width:100px">替换为<div>
比你帅

2024-09-10 08:13:51

(\<.[^\s]*)\s.[^>]*>
正则表达式