Stories

Mostly programming stories.

 
  • Home
  • Programming
    • Python
      • Django
    • PHP
    • CSS
    • Javascript
  • Linux
  • Other stories
Menu
  • Home
  • Programming
    • Python
      • Django
    • PHP
    • CSS
    • Javascript
  • Linux
  • Other stories
  • PHP – Automatic “Table of Contents” based on headers (h1, h2, etc)

    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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
     
    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

    Facebook
    Twitter
    Google+

    2 Sep 2013 / simion / 0

    Categories: PHP, Programming

    Tags: php

    Boghy + Python Bootstrap carousel – fade effect vs slide

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Please type the characters of this captcha image in the input box

    Please type the characters of this captcha image in the input box

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Categories

  • Linux (3)
  • Other stories (4)
  • Pics (1)
  • Programming (11)
    • CSS (2)
    • Javascript (1)
    • PHP (1)
    • Python (5)
      • Django (3)

Recent Posts

  • Deploy with fabric on multiple servers
  • jqPlot resize bar charts
  • Top things to do after installing Manjaro KDE
  • Ubuntu – resize window with alt-right click
  • Pip PIL error: Could not find any downloads that satisfy the requirement PIL

Tweets

Tweets by @SimionBaws
 

Tags

apache2 bootstrap carousel django fabric jqplot jquery jquery-ui kde linux manjaro php python social ssh sublime-text-3 ubuntu

Archives

  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • October 2013
  • September 2013
  • August 2013

Recent Comments

  • simion on Top things to do after installing Manjaro KDE
  • ov1d1u on Top things to do after installing Manjaro KDE
  • no2 explode extreme on Apache 2.2 Forbidden 403 error

Categories

  • Linux
  • Other stories
  • Pics
  • Programming
    • CSS
    • Javascript
    • PHP
    • Python
      • Django

Copyright © 2014 StoriesTheme created by PWT. Powered by WordPress.org