如何根据元素树结构用div包装元素?

时间:2015-06-03 10:11:54

标签: php html dom

我已经这样做了2天没有运气。我尝试使用Dom parser class和循环方法,但两者都混淆了很多,无法得到解决方案。

目的: 我需要用div包装h标签。亲子关系类似于h1-> h2-> h3,依此类推。所以,我需要根据树结构包装div。

HTML

$html='<h1>some text<h1>
sometext
<h2>some text</h2>
sometext

<h1>some text<h1>
sometext
<h2>some text</h2>
sometext
<h3>some text</h3>
sometext';

预期输出

<div class="sect1">
<h1>some text<h1>
sometext
<div class="sect2">
<h2>some text</h2>
sometext
</div>
</div>

<div class="sect1">
<h1>some text<h1>
sometext
<div class="sect2">
<h2>some text</h2>
sometext
<div class="sect3">
<h3>some text</h3>
 sometext
</div>
</div>
</div>

尝试1:

$dom = new DOMDocument();
$dom->loadHTML($html);
$elements = $dom->getElementsByTagName('*');

        for ($i = 0; $i < $elements->length; $i++) {            
        $element = $elements->item($i);
        if ($element->tagName == 'h1'){ 
           $wrap1 = $dom->createElement('div');
           $wrap1->setAttribute('class', 'sect1');

            $wrap1->appendChild($element);
            $dom->appendChild($wrap1);
        }
        if ($element->tagName == 'h2'){ 
           $wrap2 = $dom->createElement('div');
           $wrap2->setAttribute('class', 'sect2');

            $wrap2->appendChild($element);
            $wrap1->appendChild($wrap2);
            $dom->appendChild($wrap1);
        }
    }     

尝试2:

$f=file("extractedadd.xhtml");
for($i=0;$i<count($f);$i++){
    if(strpos($f,'<h1 class="title"')!==false);
    $h1[].=$i;  
}
$h2count=0;
$h3count=0;
for($j=0;$j<count($h1);$j++){
  for($k=$h1[$j];$k<$h1[$j+1];$k++){
      if(strpos($f[$k],'<h2 class="title"')!==false){$h2=$h2count+1;}
      if(strpos($f[$k],'<h3 class="title"')!==false){$h3=$h3count+1;}

 }
 if($h2count!=0){
 for($z=1;$z<=$h2count;$z++){
     $to2.="</div>\n";
 }
 $r=str_replace('<h1 class="title"',$to2."</div>\n".'<h1 class="title"',$h1[$j]);
 $to2="";
}//something like this.

请帮忙!如何获得我期望的解决方案?

1 个答案:

答案 0 :(得分:1)

我做了一个更难的结构

$html='<h1>some text<h1>
sometext
<h2>some text</h2>
sometext

<h2>some text</h2>
one more sometext

<h1>some text<h1>
sometext
<h2>some text</h2>
sometext
<h3>some text</h3>
sometext';

$level = 0;
$array = split("\n", $html); // make array of lines
foreach($array as $line)  {
  if (preg_match('/\s*\<h(\d+)/i', $line, $matches)) {
     $l = $matches[1];  // take new level from tag h
     if ($level == $l) echo "</div>\n<div class=\"sect.$l.\">\n"; // the same level
     else {
        while ($level >= $l) {echo "</div>\n"; $level--; }
        while ($level < $l) {echo "<div class=\"sect".$l."\">\n"; $level++; }
     }
  }
  echo $line."\n";   
}
While ($level >= 1) {echo "</div>\n"; $level--; } // close not closed :)

Whatch there working