A reminder to myself » PHP http://simionbaws.ro Stuff I might need later. Mon, 06 Jul 2015 13:25:52 +0000 en-US hourly 1 http://wordpress.org/?v=4.3.1 PHP – Automatic “Table of Contents” based on headers (h1, h2, etc) http://simionbaws.ro/programming/php-automatic-table-of-contents-based-on-headers-h1-h2-etc/ http://simionbaws.ro/programming/php-automatic-table-of-contents-based-on-headers-h1-h2-etc/#comments Mon, 02 Sep 2013 12:14:40 +0000 http://blog.simionbaws.ro/?p=88 Works like a charm.

Also it add automatic ids for your tags, for automatic linking.

Demo here: http://assist-software.net/blog/qualsyst-iso-management-application

function create_automatic_toc($content) {
    preg_match_all( '/<h([1-6])(.*)>([^<]+)<\/h[1-6]>/i', $content, $matches, PREG_SET_ORDER );

    global $anchors;

    $anchors = array();
    $toc     = '<ul class="toc">'."\n";
    $i       = 0;

    foreach ( $matches as $heading ) {

        if ($i == 0)
            $startlvl = $heading[1];
        $lvl        = $heading[1];

        $ret = preg_match( '/id=[\'|"](.*)?[\'|"]/i', stripslashes($heading[2]), $anchor );
        if ( $ret && $anchor[1] != '' ) {
            $anchor = stripslashes( $anchor[1] );
            $add_id = false;
        } else {
            $anchor = preg_replace( '/\s+/', '-', preg_replace('/[^a-z\s]/', '', strtolower( $heading[3] ) ) );
            $add_id = true;
        }

        if ( !in_array( $anchor, $anchors ) ) {
            $anchors[] = $anchor;
        } else {
            $orig_anchor = $anchor;
            $i = 2;
            while ( in_array( $anchor, $anchors ) ) {
                $anchor = $orig_anchor.'-'.$i;
                $i++;
            }
            $anchors[] = $anchor;
        }

        if ( $add_id ) {
            $content = substr_replace( $content, '<h'.$lvl.' id="'.$anchor.'"'.$heading[2].'>'.$heading[3].'</h'.$lvl.'>', strpos( $content, $heading[0] ), strlen( $heading[0] ) );
        }

        $ret = preg_match( '/title=[\'|"](.*)?[\'|"]/i', stripslashes( $heading[2] ), $title );
        if ( $ret && $title[1] != '' )
            $title = stripslashes( $title[1] );
        else
            $title = $heading[3];
        $title      = trim( strip_tags( $title ) );

        if ($i > 0) {
            if ($prevlvl < $lvl) {
                $toc .= "\n"."<ul>"."\n";
            } else if ($prevlvl > $lvl) {
                $toc .= '</li>'."\n";
                while ($prevlvl > $lvl) {
                    $toc .= "</ul>"."\n".'</li>'."\n";
                    $prevlvl--;
                }
            } else {
                $toc .= '</li>'."\n";
            }
        }

        $j = 0;
        $toc .= '<li><a href="#'.$anchor.'">'.$title.'</a>';
        $prevlvl = $lvl;

        $i++;
    }

    unset( $anchors );

    while ( $lvl > $startlvl ) {
        $toc .= "\n</ul>";
        $lvl--;
    }

    $toc .= '</li>'."\n";
    $toc .= '</ul>'."\n";

    return array(
        'toc' => $toc,
        'content' => $content
    );
}

Source: http://www.westhost.com/contest/php/function/create-table-of-contents/124

Google+
]]>
http://simionbaws.ro/programming/php-automatic-table-of-contents-based-on-headers-h1-h2-etc/feed/ 0