vaishu navi
vaishu navi

Reputation: 43

How to wrap the elements by div according to the elements tree structure?

I have been doing this for 2 days with no luck. I tried in both using Dom parser class and loop method but both of them confusing a lot and couldn't get a solution.

Objective: I need to wrap h tags with div. Parent-child relationship is like h1->h2->h3 and so on. So, I need to wrap div according to the tree structure.

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';

Expected output

<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>

Attempt 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);
        }
    }     

Attempt 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.

Pls help! how to get my expected solution?

Upvotes: 4

Views: 146

Answers (1)

splash58
splash58

Reputation: 26153

i make a little more difficulty structure

$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

Upvotes: 1

Related Questions