douliang7068 2018-01-17 12:17
浏览 30
已采纳

PHP DOM Document将整个表复制成一个新的div

I receive HTML table from jquery summernote like this

<div class="pasted">
   <table class="table table-bordered" style="width: 100%;">
      <tbody>
         <tr>
            <td>
              item 1
            </td>
         </tr>
       </tbody>
     </table>
</div>

Now i want to convert to this using PHP

<div class="pasted">
   <div class="table-responsive">
      <table class="table table-bordered table-summernote1">
         <tbody>
            <tr>
               <td>
                 item 1
               </td>
            </tr>
         </tbody>
        </table>
   </div>
</div>

Here is my PHP Code:

<?php

$dom = new DOMDocument();
$dom->loadHTML($text, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$b = $dom->getElementsByTagName('table');

foreach ( $b as $t )
{
    if ( $t->hasAttributes() )
    {
        foreach ( $t->attributes as $attr )
        {
            $t->removeAttribute($attr->nodeName);
        }

        $t->removeAttribute("style");

        $t->setAttribute('class', 'table table-bordered table-summernote1');

        $responsive  = $dom->createElement('div');
        $t->insertBefore($responsive);
        $responsive->setAttribute('class', 'table-responsive');

        $frag = $t->cloneNode(true);

        $responsive->appendChild($frag);
    }
}

?>

PHP code does not work. page hangs on $responsive->appendChild($frag) and processing is never ended.

I have also tried with preg_replace but does not work

foreach ( $b as $t )
{
    if ( $t->hasAttributes() )
    {
        foreach ( $t->attributes as $attr )
        {
            $t->removeAttribute($attr->nodeName);
        }

        $t->removeAttribute("style");
    }
}

$text = $dom->saveHTML();

$text = preg_replace('/<table>(.*)<\/table>/isum', '<div class="table-responsive"><table class="table table-bordered table-summernote1">$1</table></div>', $text);

It will convert just first 1 or 2 tables. if there are multiple tables, one table goes into another table.

what is the better solution to solve this?

  • 写回答

1条回答 默认 最新

  • duanhe8280 2018-01-17 12:41
    关注

    It seems to be objecting to adding the cloned node inside the node your cloning. I've changed how $responsive is added (to the parent of the table) and remove the $t node before adding the clone back in.

    foreach ( $b as $t )
    {
        if ( $t->hasAttributes() )
        {
            foreach ( $t->attributes as $attr )
            {
                $t->removeAttribute($attr->nodeName);
            }
    
            $t->removeAttribute("style");
    
            $t->setAttribute('class', 'table table-bordered table-summernote1');
    
            $responsive  = $dom->createElement('div');
            $t->parentNode->insertBefore($responsive, $t);
            $responsive->setAttribute('class', 'table-responsive');
    
            $frag = $t->cloneNode(true);
            $t->parentNode->removeChild($t);
    
            $responsive->appendChild($frag);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改