PHP简单html DOM从html标记中删除所有属性
上面是我编写的代码,用于获取html页面中所有PHP简单html DOM从html标记中删除所有属性,php,html,attributes,simple-html-dom,Php,Html,Attributes,Simple Html Dom,上面是我编写的代码,用于获取html页面中所有标记中的内容并将其删除。 我的html代码与此类似: $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->
标记中的内容并将其删除。我的html代码与此类似:
$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;
某些文本
某些文本
某些文本
某些文本
某些文本
某些文本
如果我运行php代码,结果将是:
<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>
一些文本
一些文字
一些文字
某些文本
某些文本
一些文字
它不会删除
内的标签属性
如果有人能帮我,我将不胜感激。当我使用您的代码和示例HTML时,它确实会删除所有
标记中的所有属性,甚至是
中的属性,所以我不确定您的代码为什么不起作用
但它似乎专门处理属性,因此不必使用字符串函数:
<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>
希望这会更有效。TNX 4您的答案,但它给了我一个错误:警告:为foreach()提供的参数无效((对于第二个foreach))@SAM我编辑了答案以更正错误。再试一次?
$html = file_get_html('page.php');
foreach($html->find('p') as $p) {
foreach ($p->getAllAttributes() as $attr => $val) {
$p->removeAttribute($attr);
}
}
echo $html->innertext;