PHP simple html DOM remove all attributes from an html tag

你说的曾经没有我的故事 提交于 2019-12-07 07:07:19

问题


$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;

Above is the code I wrote to take what's inside all <p> tags in my html page and remove them.


My html code is similar to this :
<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>


If I run the php code , result would be this :
<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>

It doesn't remove <p> tags attributes that are inside <font>.
If anyone can help me with this I'll be appreciate.


回答1:


When I use your code and example HTML, it does remove all the attributes from all the <p> tags, even the ones inside <font>, so I'm not sure why yours isn't working.

But it looks like simplehtmldom has methods that specifically deal with attributes so you don't have to use string functions:

$html = file_get_html('page.php');


foreach($html->find('p') as $p) {
    foreach ($p->getAllAttributes() as $attr => $val) {
        $p->removeAttribute($attr);
    }    
}
echo $html->innertext;

Hopefully that will be more effective.



来源:https://stackoverflow.com/questions/38794173/php-simple-html-dom-remove-all-attributes-from-an-html-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!