PHP简单的html DOM从html标记中删除所有属性
发布时间:2020-12-13 17:01:10 所属栏目:PHP教程 来源:网络整理
导读:$html = file_get_html('page.php');foreach($html-find('p') as $tag_name) { $attr = substr($tag_name-outertext,2,strpos($tag_name-outertext,"")-2); $tag_name-outertext = str_replace($attr,"",$tag_name-outertext); }echo $html-innertext; 以上
$html = file_get_html('page.php');
foreach($html->find('p') as $tag_name)
{
$attr = substr($tag_name->outertext,2,strpos($tag_name->outertext,">")-2);
$tag_name->outertext = str_replace($attr,"",$tag_name->outertext);
}
echo $html->innertext;
以上是我编写的代码,其中包含所有内容< p>我的HTML页面中的标签并删除它们. <p class="..." id = "..." style = "...">some text...</p>
<p class="..." id = "..." style = "...">some text...</p>
<p class="..." id = "..." style = "...">some text...</p>
<font>
<p class="..." id = "..." style = "...">some text ...</p>
<p class="..." id = "..." style = "...">some text ...</p>
</font>
<p class="..." id = "..." style = "...">some text...</p>
如果我运行php代码,结果将是这样的: <p>some text...</p>
<p>some text...</p>
<p>some text...</p>
<font>
<p class="..." id = "..." style = "...">some text ...</p>
<p class="..." id = "..." style = "...">some text ...</p>
</font>
<p>some text...</p>
它不会删除< p>标记< font>内的属性. 解决方法
当我使用您的代码和示例HTML时,它会删除所有< p>中的所有属性.标签,甚至是< font>里面的标签,所以我不确定你为什么不工作.
但它看起来像simplehtmldom has methods专门处理属性,所以你不必使用字符串函数: $html = file_get_html('page.php');
foreach($html->find('p') as $p) {
foreach ($p->getAllAttributes() as $attr => $val) {
$p->removeAttribute($attr);
}
}
echo $html->innertext;
希望这会更有效. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
