加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 15:56:23 所属栏目:PHP教程 来源:网络整理
导读:$html='h1 class="title"Exponents[label*exponents]/h1h2 class="title"Exponents[label*exponents]/h2h2 class="title"Exponents[label*exponents]/h2h1 class="title"Exponents[label*exponents]/h1h2 class="title"Exponents[label*exponents]/h2h2 clas
$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那么它是第1章.然后第二次出现h1,第二次出现h2.
当我正在学习DOM解析器类时,我尝试过类似下面的内容,

$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;
     }           
}

我不知道是否正确的方法来获得我的预期输出,如果它是正确的,我不知道如何做进一步.

解决方法

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

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

$xp = new DOMXPath($dom);
$xp->registerNamespace("php",'$className')]");
$chapno=1;
$h = array(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,$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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读