html - DOM php delete all tags by tag name -
i'm having problem script i've been using time , worked me until hit problem.
i have script i'd delete p html tags html source code. script work partly because removes of p tags, leaves out.
i don't understand why that.
$doc = new domdocument(); $a = <<<fail <html><body> <div style="clear:both"></div> <p class="articletitle">hoo</p> <p class="articletext">hmmm</p> <p class="articletext">hmmmm</p> <p align="center"></p> </body></html> fail; $doc->loadhtml($a); $list = $doc->getelementsbytagname("p"); foreach ($list $l) { $l->parentnode->removechild($l); $c++; } echo $doc->savehtml() . $c;
the script returns
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/tr/rec-html40/loose.dtd"> <html><body> <div style="clear:both"></div> <p class="articletext">hmmm</p> <p align="center"></p>
leaving out 2 p tags...
can please me find out why it's skipping tags
try way:
$doc->loadhtml($a); $list = $doc->getelementsbytagname("p"); while ($list->length > 0) { $p = $list->item(0); $p->parentnode->removechild($p); }
Comments
Post a Comment