Use PHP to generate table of contents from heading tags

David Carr


I wanted to create a table of contents from the post's heading tags automatically without having to change the headings like adding an id or anchor.

I started to look at existing solutions and came across this custom function on Stackoverflow by sjaak-wish

function generateIndex($html) {

    $index = "<ul>";
    $prev = 2;

    foreach ($matches[0] as $i => $match){

        $curr = $matches[1][$i];
        $text = strip_tags($matches[2][$i]);
        $slug = strtolower(str_replace("--","-",preg_replace('/[^\da-z]/i', '-', $text)));
        $anchor = '<a name="'.$slug.'">'.$text.'</a>';
        $html = str_replace($text,$anchor,$html);

        $prev <= $curr ?: $index .= str_repeat('</ul>',($prev - $curr));
        $prev >= $curr ?: $index .= "<ul>";

        $index .= '<li><a href="#'.$slug.'">'.$text.'</a></li>';

        $prev = $curr;

    $index .= "</ul>";

    return ["html" => $html, "index" => $index];

This will generate a list of links based on the headings.

To use the function call it and pass in your content, then specify the array key to use. html for the body and index for the table of contents.

Table of contents:





Fathom Analytics $10 discount on your first invoice using this link

Help support the blog so that I can continue creating new content!

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2023 DC Blog. All code MIT license. All rights reserved.