【问题标题】:How to replace all ul and li tag with div using PHP Simple HTML DOM Parser?如何使用 PHP Simple HTML DOM Parser 用 div 替换所有 ul 和 li 标签?
【发布时间】:2013-10-28 07:35:12
【问题描述】:

好的,我想使用 PHP Simple HTML DOM Parser 创建一个“网站动员”。在现阶段,我想-

  1. 将所有'ul'和'li'标签改为'div'标签和
  2. 将所有“表格”元素(例如 table、tr、td、th)更改为 div。我通过以下方式尝试了第一个问题的解决方法:

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

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

这似乎很枯燥,但我无法找到任何其他解决方案。我不鼓励使用preg_match,虽然我不知道它是否能给我想要的输出。任何帮助将不胜感激。

【问题讨论】:

  • +1 用于使用 DOM 解析器而不是正则表达式。
  • 是否可以使用 PHP Simple HTML DOM Parserul 标签更改为 div 标签?如果是,我在这里错过了什么?

标签: php preg-match simple-html-dom


【解决方案1】:

有可能:

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

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

当然你也可以对表格做同样的事情。 更多文档: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>"
    }
    

    这会将$html DOM 中$replace 数组中的所有元素替换为&lt;div&gt;,而不会更改这些标签的内容。一切都存储在$html DOM 中。

    如您所见,您无法使用$element 更改$html 中的任何内容,即使使用$element 作为参考,您也必须直接访问$html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多