Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Use PHP to generate table of contents from heading tags

David Carr

PHP & MySQL

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) {
    preg_match_all('/<h([1-6])*[^>]*>(.*?)<\/h[1-6]>/',$html,$matches);

    $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:

toc($post->content)['index']

Content:

toc($post->content)['html']

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

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

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

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