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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?