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

☆樱花仙子☆ 提交于 2020-01-15 03:30:08

问题


Ok, I want to create a "website mobilizer" by using PHP Simple HTML DOM Parser. In the present phase, I want to-

  1. change all 'ul' and 'li' tag to 'div' tag and
  2. change all 'table' elements (e.g. table,tr,td,th) to div. I tried an workaround for the first problem in following way:

.

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

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

It does seem dull, but I'm not being able to find any other solution. I am discouraged for using preg_match, though I don't know if it can give me the desired output. Any help will be appreciated.


回答1:


It is possible:

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

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

Of course you can do the same with table. More in doucmetation: Simple HTML DOM Parser Manual




回答2:


$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>"
}

this replaces all the elements from $replace array to <div> in $html DOM without changing the contents of those tags. Everything is stored in $html DOM.

As you can see you cant use $element to change anything in $html, even using $element as a reference, so you have to access $html directly.



来源:https://stackoverflow.com/questions/19629289/how-to-replace-all-ul-and-li-tag-with-div-using-php-simple-html-dom-parser

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