How to replace all ul and li tags with div using PHP Simple HTML DOM Parser?

Ok, I want to create a "site mobilizer" using the PHP Simple HTML DOM Parser. At this stage, I want:

  • change all the tags 'ul' and 'li' to the tag 'div' and
  • change all table elements (e.g. table, tr, td, th) to a div. I tried a workaround for the first problem as follows:

.

$html=new new simple_html_dom();
$html>load_file($sourceurl);

$div="div";
foreach ($html->find('ul') as $element) {  
 $element=$div;
 }

It seems boring, but I cannot find another solution. I am not recommended to use preg_match, although I do not know if it can give me the desired result. Any help would be appreciated.

+2
source share
2 answers
$html=new new simple_html_dom();
$html>load_file($sourceurl);

$replace="ul,li,table,tr,td,th";  
foreach($html->find($replace) as $key=>$element){
    $html->find($replace,$key)->outertext="<div>".$element->innertext."</div>"
}

$replace <div> $html DOM . $html DOM.

, $element - $html, $element , $html.

+3

:

$html=new new simple_html_dom();
    $html>load_file($sourceurl);

    foreach ($html->find('ul') as $element) {  
     $element->innertext = "<div>".$element->innertext."</div>";     
    }

, . doucmetation: HTML DOM Parser Manual

+2

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


All Articles