Create a table of contents automatically.

Posted on 2003-11-14
Medium Priority
Last Modified: 2008-03-17
Ok, here's what I want to do: my site has a ot of articles and they are all generally organized by h3 header tags for section headings (and I can clean it up to strictly enforce that). Now I am thinking that I want to add a TOC to the top of the article, in the sense of:

1. Introduction
2. Blah Blah
3. More Blah Blah
4. Conclusion

Each one would be a link to an anchor in the article.

So I need to 1) parse the article and add anchor tags to the headings and 2) build a TOC. I have a good idea of how to do this in other languages, but I am still pretty new to PHP and I am sure I would miss built-in functions that would help a lot. Any suggestions?
Question by:Squeebee
  • 5
  • 4

Assisted Solution

red010knight earned 400 total points
ID: 9751041
The functions that should help you in this are:

1>This function will help you parse the file
Reads entire file into an array (PHP 3, PHP 4 )
array file ( string filename [, int use_include_path] )

Identical to readfile(), except that file() returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns FALSE.


2>To set up the anchor tags - something like this would do the job:

      //0 may need to be changed to 3 on the above line
      //addslashes used in case of ' or " in the string
  $newString="<h3><a name=\"".$markStr."\">".$markStr."</a></h3>";
}//of if
}//of for loop

3>To generate the file TOC, you will need to have a loop
    echo("<a href=\""filename.php#".$tocMark[$tocLine]."\">".$tocMark[$tocLine]."</a><br>\n");

4>Once you get it to work I would recommend saving the source code as the new file so that your server isn't overloaded with generating TOC's all the time. But I don't know your situation so its only a recommendation.

Expert Comment

ID: 9751069
Oh and to Get the file back out properly you can either make the line:
echo($sting) or echo(addslashes($sting) OR once out of the loop you should be able to do:

and that should do the rest...
I believe this code will do the trick - if you have any problems just make a post and I will fix it up.


LVL 17

Author Comment

ID: 9751115
Oh, I didn't mention that the article will be a text  column in a mysql query, so we are assuming $row['article'] as the source of the article.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

LVL 17

Author Comment

ID: 9751119
Sorry, that piece of information changes things I suppose.

Expert Comment

ID: 9751174
Just a little and makes it a whole lot easier tooo.... SHEESH... but the concept is basically the same...

Why not give each article its own table, then divide the article up into sections in the table where the table row has the values:
title text

then a select * from articleName

while blah = blahres
$tocString.="<a href=\""filename.php#".$tocMark[$tocLine]."\">".$tocMark[$tocLine]."</a><br>\n";
$bodyString="<h3><a name=\"".$blah['title']."\">".$blah['title']."</a></h3>\n".$blah['body'];
echo $tocString;
echo $bodyString;

and your page is generated

Expert Comment

ID: 9751192
and then if you wanted to have sub headings it wouldn't matter in the display much because you could just have another field that is 'level' that is either h1, h2, h3, h4, h5, or h6 and you can include that in the bodyString accordingly

LVL 17

Author Comment

ID: 9751220
Sorry, I am new to PHP but no to databases. A table per article would not be a normalized approach, and after screaming 'NORMALIZE OR DIE!' in the MySQL topic area I would look like a pretty big hypocrite to go with a table per article. That and the backend is very established are far as the DB is concerned, I am just adding a TOC onto the existing system.
LVL 11

Accepted Solution

shmert earned 1600 total points
ID: 9752241
I actually saw a really slick javascript that did this, using the DOM model.  Here's something in PHP:

$url = 'http://localhost/php/article.php';
$raw = file_get_contents($url);
$raw = preg_replace_callback('|<h3>([^<]*)</h3>|i', 'tocCallback', $raw);
echo '<ol>';
foreach($toc AS $key=>$value) {
        echo '<li><a href="#part' . ($key + 1) . '">' . $value . '</a></li>';
echo '</ol>';
echo $raw;

function tocCallback($matches) {
        global $toc;
        if (!$toc) $toc = array();
        $toc[] = $matches[1];
        $anchorName = 'part' . count($toc);
        return "<a name=\"$anchorName\"></a>" . $matches[0];
LVL 17

Author Comment

ID: 9752404
Now that works extremely well! As Red010Knight provided a solution that would have worked had I given enough detail, I'm going to split a few his way.
LVL 17

Author Comment

ID: 10068383
Followup for this at http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20844380.html
Another 500 points available.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question