drake900918 2015-03-07 23:23
浏览 58
已采纳

用dom php创建的不需要的div

Can create primary div without xml tags displaying and without any problem

$myfile = fopen("../userfolders/$email/$ongrassdb/$pagenameselected.php", "a+") or die("Unable to open file!");
$dom = new DOMDocument();
$ele = $dom->createElement('div', $textcon);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-col; width :100%;');
$dom->appendChild($ele);
$html = $dom->saveHTML();
fwrite($myfile,$html);
fclose($myfile);

Trying to create child div but the below code creates duplicates of parent div and child div and adds XML tags after every div

$myfile = fopen("../userfolders/$email/$ongrassdb/$pagenameselected.php", "a+") or die("Unable to open file!");
$file = "../userfolders/$email/$ongrassdb/$pagenameselected.php";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$ele = $doc->createElement('div', $textcon);
$element = $doc->getElementsByTagName('div')->item(0);
$element->appendChild($ele);
$ele ->setAttribute('id', $divname);
$ele ->setAttribute('style', 'background: '.$divbgcolor.'; color :'.$divfontcolor.' ;display : table-cell;');
$doc->appendChild($ele);
$html = $doc->saveHTML();
fwrite($myfile,$html);
  • 写回答

3条回答 默认 最新

  • doudouwd2017 2015-03-08 01:47
    关注

    update

    How about this

    You will have three files, $outputfile will be a new file to save the html to. the $parentfile and $childfile are the two files you want to pull divs out of . $pId is the ID of the parent div, $cId is the ID of the child div (which should have been set when the files were created if im not mistaken)

    $outputfile = '/path/to/new/output/file/';
    $parentfile = '/path/to/file/with/parent/div/';
    $childfile = '/path/to/file/with/child/div/';
    $pId = 'myParentDivId';
    $cId = 'myChildDivId';
    
    $file = $parentfile;
    
    $p = new DOMDocument();
    $p->loadHTMLFile($file);
    $pEle = $p->getElementById($pId);
    
    $file = $childfile;
    
    $c = new DOMDocument();
    $c->loadHTMLFile($file);
    $cEle = $c->getElementById($cId);
    
    $pEle->appendChild($cEle);
    
    $m = new DOMDocument();
    
    $m->appendChild($pEle);
    
    $myfile = fopen($outputfile, "a+") or die('bye');
    $html = $m->saveHTML();
    fwrite($myfile,$html);
    fclose($myfile);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答