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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?