2016-07-13 187 views
2

使用PHP,我试图在分析的HTML中添加div,插入位置取决于现有标签的ID。使用PHP插入div到HTML文件

我得到一个基本上只读的生成的HTML文件(我无法控制)。 我的工作是获取该HTML文件,添加几个div(按钮和其他东西),并重新发回。

这是有点棘手的地方:我需要添加div(我用按钮和东西添加的那些)取决于可以在HTML文件中多次出现的div ID。

为了让我添加的div正常工作,它必须添加在标记的,可识别的div的父div之前。

简单地说:

... some html 

<--- where i want to put our lovely div 

<div> // no tags no info just a simple div 

    <div id="myId"> 
     ... some html 
    </div> 

</div> 

... some html 
+0

一个例子就好了 – Jah

+0

好即时通讯试图找到一种方法来搜索HTML文件中的标签获取父标签,并在它之前插入一串代码。 得到父标签是困扰我的一个问题 – Z3R0

+0

为什么不构造一个if/else语句?如果($ divID ==='abc'){insert ...} else {do something instead} – Kray

回答

3

你绝对是最好的选择,国际海事组织将使用PHP的原生DOM文档:http://php.net/manual/en/class.domdocument.php

有相当的学习曲线,这让我激动的东西,应该让你去在正确的方向 - 如果不提​​供一个解决方案。我已经包括逐行评论这里要说明每一个步骤:

// the filename you want to parse 
$filename = './test.html'; 

// an array of replacement html snippets and the id of the child element. 
// html will be inserted before parent of each child with a matching ID as you described 
$replacements = [ 
    [ 
     'id'  => 'myId', 
     'insert' => '<button>Insert before parent of #myId</button>' 
    ], 
    [ 
     'id'  => 'myId2', 
     'insert' => '<button>Insert before parent of#myId2</button>' 
    ] 
]; 

// instantiate DOMDocument and read the html file 
libxml_use_internal_errors(true); 
$dom = new DOMDocument(); 
$dom->loadHTMLFile('./test.html'); 

// get an array of all dom elements 
$elements = $dom->getElementsByTagName('*'); 

// iterate through dom elements 
foreach($elements as $element) { 

    // check if this element has an 'id' attribute 
    if ($element->hasAttribute('id')) { 

     // iterate through replacement array 
     foreach ($replacements as $i => $replacement) { 

      // if element's id is a match then add this node to our array 
      if ($element->getAttribute('id') == $replacement['id']) { 
       $replacements[$i]['nodes'][] = $element; 
      } 
     } 
    } 
} 

// iterate through replacements again 
foreach ($replacements as $replacement) { 

    // iterate through nodes we found which matched 
    foreach ($replacement['nodes'] as $node) { 

     // create a DOMDocument node from an html string 
     $html = $dom->createDocumentFragment(); 
     $html->appendXML($replacement['insert']); 

     // insert this node before parent 
     $node->parentNode->parentNode->insertBefore($html,$node->parentNode); 
    } 
} 

// output the revised html 
echo $dom->saveHTML(); 

// note - if your html doesn't have <html> and <body> tags they will be automatically added by DOMDOcument 
// you can work around this and get only body innerhtml with something like this 
echo str_replace(['<body>','</body>'],'',$dom->saveHTML($dom->getElementsByTagName('body')->item(0))); 

我创建一个名为test.html的以下HTML测试文档。我故意用myId两次证明这将在事实上比赛无论有效性的每一个元素:

<div> // no tags no info just a simple div 
    <div id="myId"> 
     ... some html 
    </div> 
</div> 

<div> // no tags no info just a simple div 
    <div id="myId2"> 
     ... some html 
    </div> 
</div> 

<div> // no tags no info just a simple div 
    <div id="myId"> 
     ... some html 
    </div> 
</div> 

PHP代码上面的输出如下:

<button>Insert before parent of #myId</button><div> // no tags no info just a simple div 
    <div id="myId"> 
     ... some html 
    </div> 
</div> 

<button>Insert before parent of #myId2</button><div> // no tags no info just a simple div 
    <div id="myId2"> 
     ... some html 
    </div> 
</div> 

<button>Insert before parent of #myId</button><div> // no tags no info just a simple div 
    <div id="myId"> 
     ... some html 
    </div> 
</div> 
+0

Thx很多完美的作品 – Z3R0