如何在PHP中使用DOM解析器附加文本(递增顺序)?

时间:2015-06-09 11:24:34

标签: php html dom domxpath

$html='<h1 class="title">Exponents[label*exponents]</h1>
<h2 class="title">Exponents[label*exponents]</h2>
<h2 class="title">Exponents[label*exponents]</h2>
<h1 class="title">Exponents[label*exponents]</h1>
<h2 class="title">Exponents[label*exponents]</h2>
<h2 class="title">Exponents[label*exponents]</h2>
<h2 class="title">Exponents[label*exponents]</h2>
<h3 class="title">Exponents[label*exponents]</h3>
<h1 class="title">Exponents[label*exponents]</h1>
<h1 class="title">Exponents[label*exponents]</h1>
<h2 class="title">Exponents[label*exponents]</h2>';

预期输出:

<h1 class="title">1.1 Exponents[label*exponents]</h1>
<h2 class="title">1.1.1 Exponents[label*exponents]</h2>
<h2 class="title">1.1.2 Exponents[label*exponents]</h2>
<h1 class="title">1.2 Exponents[label*exponents]</h1>
<h2 class="title">1.2.1 Exponents[label*exponents]</h2>
<h2 class="title">1.2.2 Exponents[label*exponents]</h2>
<h2 class="title">1.2.3 Exponents[label*exponents]</h2>
<h3 class="title">1.2.3.1 Exponents[label*exponents]</h3>
<h1 class="title">1.3 Exponents[label*exponents]</h1>
<h1 class="title">1.4 Exponents[label*exponents]</h1>
<h2 class="title">1.4.1 Exponents[label*exponents]</h2>;

例如,如果附加文本为1.1,则第一个1为第0章。第二个 1首次出现h1,如果是1.2.2,那么它是第0章。然后第二次出现h1,第二次出现h2。 正如我正在学习DOM parser class我在下面尝试过类似的内容,

$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xp = new DOMXPath($dom);
$xp->registerNamespace("php", "http://php.net/xpath");
$className="title";
$hElement = $xp->query("//*[contains(@class, '$className')]");
$chapno=1;
$h1=0;
$h2=0;
$h3=0;
$h4=0;

foreach($hElement as $hNode) {
     if($hElement==="h1"){
         $h1++;
         $append="$chapno.$h1 ";
         $hnode->nodeValue =$append.$hNode->textContent;
         $h2=0;$h3=0;$h24=0;         
     }
     if($hElement==="h2"){
         $h2++;
         $append="$chapno.$h1.$h2 ";
         $hnode->nodeValue =$append.$hNode->textContent;
     }

     if($hElement==="h3"){
         $h3++;
         $append="$chapno.$h1.$h2.$h3 ";
         $hnode->nodeValue =$append.$hNode->textContent;
     }

     if($hElement==="h4"){
         $h4++;
         $append="$chapno.$h1.$h2.$h3.$h4 "; 
         $hnode->nodeValue =$append.$hNode->textContent;
     }           
}

我不知道获得预期输出是否正确,如果是正确的,我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

我简化了代码,然后才能理解它。我希望一切都清楚。

$dom = new DOMDocument;
$dom->loadHTML($html);

$xp = new DOMXPath($dom);
$xp->registerNamespace("php", "http://php.net/xpath");
$className="title";
$hElement = $xp->query("//*[contains(@class, '$className')]");
$chapno=1;
$h = array(0, 0, 0, 0); 

foreach($hElement as $hNode) {
    if ($hNode->nodeName[0] === 'h') {     // 1st letter is tag name
       $i =  $hNode->nodeName[1];          // level number 

       $h[$i-1]++;                         // increase counter of level
       $append = $chapno.".".implode('.', array_slice($h, 0, $i));
       while($i < count($h)) $h[$i++] = 0; // set lower levels with start value 
       $hNode->nodeValue = $append." ".$hNode->nodeValue;  // Don't forget to change title text
    }
}   

echo $dom->saveHTML();

结果

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><h1 class="title">1.1 Exponents[label*exponents]</h1>
<h2 class="title">1.1.1 Exponents[label*exponents]</h2>
<h2 class="title">1.1.2 Exponents[label*exponents]</h2>
<h1 class="title">1.2 Exponents[label*exponents]</h1>
<h2 class="title">1.2.1 Exponents[label*exponents]</h2>
<h2 class="title">1.2.2 Exponents[label*exponents]</h2>
<h2 class="title">1.2.3 Exponents[label*exponents]</h2>
<h3 class="title">1.2.3.1 Exponents[label*exponents]</h3>
<h1 class="title">1.3 Exponents[label*exponents]</h1>
<h1 class="title">1.4 Exponents[label*exponents]</h1>
<h2 class="title">1.4.1 Exponents[label*exponents]</h2></body></html>