亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

php: 如何將 HTML 標題 (h2,h3,h4) 轉換為嵌套無序列表?

php: 如何將 HTML 標題 (h2,h3,h4) 轉換為嵌套無序列表?

慕碼人8056858 2023-12-11 10:40:04
輸入數組,其中包含 WordPress 帖子中的 html 標題<?php$headings = array(0 => "<h2>Number One</h2>",1 => "<h2>Number Two</h2>",2 => "<h2>Number Three</h2>",3 => "<h3>Number Four</h3>",4 => "<h2 id='iamanexistingid'>Number Five</h2>",5 => "<h3>Number Six</h4>",6 => "<h4>Number Seven</h2>",7 => "<h2>Number Eight</h2>");現在我想在嵌套無序列表中生成一個結構。輸出<ul><li>Number One</li><li>Number Two</li><li>Number Three   <ul>      <li>Number Four</li>   </ul><li>Number Five</li><li>Number Five   <ul>      <li>Number Six         <ul>            <li>Number Seven</li>         </ul>     </li>   </ul></li><li>Number Eight</li></ul>有沒有簡單的方法可以使用 Simple HTML DOM Parser 生成它?還是詭計?我最初的解決方案對于比一級更深的級別實際上不起作用,并且總體來說不好。我也不知道如何處理 H3 之后的 H4 或 H3 之后的 H3。<?php$headings = array(0 => "<h2>H2 Number One</h2>",1 => "<h2>H2 Number Two</h2>",2 => "<h2>H2 Number Three</h2>",3 => "<h3>H3 Number Four</h3>",4 => "<h2 id='iamanexistingid'>H2 Number Five</h2>",5 => "<h3>H3 Number Six</h4>",6 => "<h4>H4 Number Seven</h2>",7 => "<h3>H3 Number Eight</h2>",8 => "<h2>H2 Number Nine</h2>");$array = array();$lastlevel = 2;foreach ($headings as $key => $value) {  preg_match("/\<(?<name>\w+)(?<attributes>\s+[^>]*|)>/", $value, $matches);  $tagname = $matches[1];  $currentlevel = str_replace("h", "", "$tagname");  if($currentlevel == $lastlevel OR $currentlevel < $lastlevel) {      array_push($array, $value);  } elseif ($currentlevel > $lastlevel) {      array_push($array, [$value]);  }  $lastlevel = $currentlevel;}echo printArrayList($array);function printArrayList($array){    echo "<ul>";    foreach($array as $k => $v) {        if (is_array($v)) {            printArrayList($v);            continue;        }        echo "<li>" . $v . "</li>";    }    echo "</ul>";}
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

function generateToc($matches) {

? ? $list ='';

? ? $current_depth? ? ? = 7;

? ? $numbered_items? ? ?= array();

? ? $numbered_items_min = null;


? ? array_walk($matches,"removelines");


? ? // find the minimum heading to establish our baseline

? ? //for ( $i = 0; $i < count( $matches ); $i ++ ) {

? ? foreach ( $matches as $i => $match ) {

? ? ? if ( $current_depth > $matches[ $i ][2] ) {

? ? ? ? $current_depth = (int) $matches[ $i ][2];

? ? ? }

? ? }


? ? $numbered_items[ $current_depth ] = 0;

? ? $numbered_items_min? ? ? ? ? ? ? ?= 7;


? ? foreach ( $matches as $i => $match ) {


? ? ? $level = $matches[ $i ][2];

? ? ? $count = $i + 1;


? ? ? if ( $current_depth == (int) $matches[ $i ][2] ) {


? ? ? ? $list .= '<li>';

? ? ? }


? ? ? // start lists

? ? ? if ( $current_depth != (int) $matches[ $i ][2] ) {


? ? ? ? for ( $current_depth; $current_depth < (int) $matches[ $i ][2]; $current_depth++ ) {


? ? ? ? ? $numbered_items[ $current_depth + 1 ] = 0;

? ? ? ? ? $list .= '<ul><li>';

? ? ? ? }

? ? ? }


? ? ? $list .= strip_tags($match);


? ? ? // end lists

? ? ? if ( $i != count( $matches ) - 1 ) {


? ? ? ? if ( $current_depth > (int) $matches[ $i + 1 ][2] ) {


? ? ? ? ? for ( $current_depth; $current_depth > (int) $matches[ $i + 1 ][2]; $current_depth-- ) {


? ? ? ? ? ? $list .= '</li></ul>';

? ? ? ? ? ? $numbered_items[ $current_depth ] = 0;

? ? ? ? ? }

? ? ? ? }


? ? ? ? if ( $current_depth == (int) @$matches[ $i + 1 ][2] ) {


? ? ? ? ? $list .= '</li>';

? ? ? ? }

? ? ? }

? ? }


? ? return '<ul class="toc">' . $list . "</li></ul>";

}


echo generateToc($headings);


查看完整回答
反對 回復 2023-12-11
  • 1 回答
  • 0 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號