'dafanzhangbb.md', 'max_title_length' => 200, //避免递归太深 'title_mark' => '一章.', 'first_title_mark' => '※', ); $_config = array(); $_config$config'title_mark' = array(); if (!$config'first_title_mark') $config'first_title_mark' = ' '; $id2array = array(); ## Data 预处理 # 读取文件 $contents = readfile($config'file'); # 字符串 分割拼接 $contents = split_contents(split_contents($contents)); ## 递归 $tree = array(); get_tree($_config, $contents, $tree); ## 生成 树 $html = ''; while ($v = current($tree)) { if (isset($v'section')) {// first title if ($v'mark' != $config'first_title_mark') { $id = $v'id'; $ids$id = (isset($ids$id) ? $ids$id+1 : 1); $html.= '
' . $v'mark' . $ids$id . '. ' . $v'title' . '
'; $html.= '
' . get_substr($v'content') . '
'; } } else {// section if ($v'mark' != $config'first_title_mark') { $html.= '
' . $v'mark' . '. ' . $v'title' . '
'; $html.= '
' . get_substr($v'content') . '
'; } } next($tree); } echo '
大番长bbb
'; echo $html; echo '
End
'; echo '
'; echo '
'; function get_substr($content = '') { $content = substr($content, 0, $GLOBALS'config''max_title_length'); return $content; } ## function readfile($filename) { return trim(file_get_contents($filename)); } function split_contents($contents = '') { return array_filter(array_unique(split("\n", $contents))); } ## function get_tree($_config, $contents, &$tree) { while ($id=$contents) { $contents_v = split_contents($_config$id$id); $mark = array_shift($contents_v); if (!$mark || $mark == $GLOBALS'config''first_title_mark') { if (!$contents_v) continue; $contents_v = $_config$id = array( 'section' => '', 'id' => $id, 'mark' => $GLOBALS'config''first_title_mark', 'contents' => $contents_v0, 'title' => 'start: ' . $contents_v0, 'content' => isset($contents_v1) ? $contents_v1 : '', 'tree' => array(), 'level_key' => $id . '※', ); } else { $contents_v = $_config$id = array( 'section' => 'section', 'id' => $id, 'mark' => $mark, 'contents' => isset($contents_v3) ? $contents_v3 : $contents_v1, 'title' => trim($contents_v1), 'content' => isset($contents_v3) ? $contents_v3 : '', 'tree' => array(), 'level_key' => $id . $mark, ); } $tree = $_config$id; if ($contents_v'content') { if ($contents_v'level_key') { get_tree($_config$contents_v'level_key', $contents_v'contents', $tree$id'tree'); } else { get_tree($_config$contents_v'id', $contents_v'contents', $tree$id'tree'); } } } } ?>