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);
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报