PHP simple html DOM removes all attributes from html tag

$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 the tags <p>on my html page and delete 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 php code, the result will be the following:
<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 does not remove the attributes of tags <p>that are inside <font>.
If anyone can help me, I will be grateful.

+4
source share
1 answer

When I use your code and HTML example, it removes all attributes from all tags <p>, even those that are inside <font>, so I'm not sure why you are not working.

, simplehtmldom , , :

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


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

, .

+2

Source: https://habr.com/ru/post/1650328/


All Articles