I have got a php file which uses an include
function to import the article. The article is a php file. I would like to create a table_of_contents()
function, which I place inside the file where I want a contents list to appear (like on Wikipedia articles). This function should scan the article for all <h2>
tags with IDs beginning with "article-
", and output a list of these headings as links.
Eg:
<h1>Article</h1>
<?php table_of_contents() ?>
<h2 id="article-intro">Introduction</h2>
<p>text</p>
<h2>Middle</h2>
<p>text</p>
<h2 id="article-conclusion">Conclusion</h2>
<p>text</p>
would render
<h1>Article</h1>
<ul>
<li><a href="#article-intro">Introduction</a></li>
<li><a href="#article-conclusion">Conclusion</a></li>
</ul>
<h2 id="article-intro">Introduction</h2>
<p>text</p>
<h2>Middle</h2>
<p>text</p>
<h2 id="article-conclusion">Conclusion</h2>
<p>text</p>