我希望在php中创建一个非常简单、非常基本的嵌套目录,它得到所有的h1-6并适当地缩进内容。这意味着如果我有这样的东西:
<h1>content</h1>
<h2>more content</h2>我应该得到:
content
more content.我知道创建缩进的将是css,这很好,但是如何创建一个带有指向页面上内容的工作链接的目录呢?
显然很难理解我想要什么.
我要求一个函数来读取html文档,并提取出所有的h1-6,并制作一个目录。
发布于 2014-05-26 02:57:08
为此,您只需搜索HTML代码中的标记即可。
我编写了两个函数(PHP5.4.x)。
第一个返回一个数组,该数组包含目录的数据。数据只是标题本身,标签的id (如果你想使用锚)和一个子表的内容。
function get_headlines($html, $depth = 1)
{
if($depth > 7)
return [];
$headlines = explode('<h' . $depth, $html);
unset($headlines[0]); // contains only text before the first headline
if(count($headlines) == 0)
return [];
$toc = []; // will contain the (sub-) toc
foreach($headlines as $headline)
{
list($hl_info, $temp) = explode('>', $headline, 2);
// $hl_info contains attributes of <hi ... > like the id.
list($hl_text, $sub_content) = explode('</h' . $depth . '>', $temp, 2);
// $hl contains the headline
// $sub_content contains maybe other <hi>-tags
$id = '';
if(strlen($hl_info) > 0 && ($id_tag_pos = stripos($hl_info,'id')) !== false)
{
$id_start_pos = stripos($hl_info, '"', $id_tag_pos);
$id_end_pos = stripos($hl_info, '"', $id_start_pos);
$id = substr($hl_info, $id_start_pos, $id_end_pos-$id_start_pos);
}
$toc[] = [ 'id' => $id,
'text' => $hl_text,
'sub_toc' => get_headlines($sub_content, $depth + 1)
];
}
return $toc;
}第二个返回用HTML格式化toc的字符串。
function print_toc($toc, $link_to_htmlpage = '', $depth = 1)
{
if(count($toc) == 0)
return '';
$toc_str = '';
if($depth == 1)
$toc_str .= '<h1>Table of Content</h1>';
foreach($toc as $headline)
{
$toc_str .= '<p class="headline' . $depth . '">';
if($headline['id'] != '')
$toc_str .= '<a href="' . $link_to_htmlpage . '#' . $headline['id'] . '">';
$toc_str .= $headline['text'];
$toc_str .= ($headline['id'] != '') ? '</a>' : '';
$toc_str .= '</p>';
$toc_str .= print_toc($headline['sub_toc'], $link_to_htmlpage, $depth+1);
}
return $toc_str;
}这两个函数远非完美,但它们在我的测试中工作得很好。你可以自由地改进它们。
注意:get_headlines不是解析器,所以它不能处理破损的HTML代码和崩溃。它也只适用于小写<hi>-tags。
发布于 2021-10-10 11:09:40
我用了这个包,它很简单,很直接的使用。
https://github.com/caseyamcl/toc
通过在composer.json文件中包含以下内容,通过Composer安装:
{
"require": {
"caseyamcl/toc": "^3.0",
}
}或者,将src文件夹放到应用程序中,并使用PSR-4自动加载程序来包含这些文件。
使用此包包含两个主要类:
TOC\MarkupFixer:将id锚属性添加到任何尚未使用的H1...H6标记(您可以指定运行时要使用的标头标记级别) TOC\TocGenerator:从HTML基本示例生成目录:
$myHtmlContent = <<<END
<h1>This is a header tag with no anchor id</h1>
<p>Lorum ipsum doler sit amet</p>
<h2 id='foo'>This is a header tag with an anchor id</h2>
<p>Stuff here</p>
<h3 id='bar'>This is a header tag with an anchor id</h3>
END;
$markupFixer = new TOC\MarkupFixer();
$tocGenerator = new TOC\TocGenerator();
// This ensures that all header tags have `id` attributes so they can be used as anchor links
$htmlOut = "<div class='content'>" . $markupFixer->fix($myHtmlContent) . "</div>";
//This generates the Table of Contents in HTML
$htmlOut .= "<div class='toc'>" . $tocGenerator->getHtmlMenu($myHtmlContent) . "</div>";
echo $htmlOut;这将产生以下输出:
<div class='content'>
<h1 id="this-is-a-header-tag-with-no-anchor-id">This is a header tag with no anchor id</h1>
<p>Lorum ipsum doler sit amet</p>
<h2 id="foo">This is a header tag with an anchor id</h2>
<p>Stuff here</p>
<h3 id="bar">This is a header tag with an anchor id</h3>
</div>
<div class='toc'>
<ul>
<li class="first last">
<span></span>
<ul class="menu_level_1">
<li class="first last">
<a href="#foo">This is a header tag with an anchor id</a>
<ul class="menu_level_2">
<li class="first last">
<a href="#bar">This is a header tag with an anchor id</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>发布于 2022-11-11 19:18:39
这个怎么样(虽然它只能做一个H级).
function getTOC(string $html, int $level=1) {
$toc="";
$x=0;
$n=0;
$html1="";
$safety=1000;
while ( $x>-1 and $safety-->0 ) {
$html0=strtolower($html);
$x=strpos($html0, "<h$level");
if ( $x>-1 ) {
$y=strpos($html0, "</h$level>");
$part=strip_tags(substr($html, $x, $y-$x));
$toc .="<a href='#head$n'>$part</a>\n";
$html1.=substr($html,0,$x)."<a name='head$n'></a>".substr($html, $x, $y-$x+5)."\n";
$html=substr($html, $y+5);
$n++;
}
}
$html1.=$html;
$html=$toc."\n<HR>\n".$html1;
return $html;
}这将创建一个基本的链接列表。
$html="<html><body>";
$html.="<h1>Heading 1a</h1>One Two Three";
$html.="<h2>heading 2a</h2>Four Five Six";
$html.="<h1 class='something'>Heading 1b</h1>Seven Eight Nine";
$html.="<h2>heading 2b</h2>Ten Eleven Twelve";
$html.="</body></html>";
echo getTOC($html, 1);给..。
<a href='#head0'>Heading 1a</a>
<a href='#head1'>Heading 1b</a>
<HR>
<html><body><a name='head0'></a><h1>Heading 1a</h1>
One Two Three<h2>heading 2a</h2>Four Five Six<a name='head1'></a><h1
class='something'>Heading 1b</h1>
Seven Eight Nine<h2>heading 2b</h2>Ten Eleven Twelve</body></html>有关正在运行的示例,请参见https://onlinephp.io/c/fceb0
https://stackoverflow.com/questions/15628305
复制相似问题