Solved

Create a table of contents automatically.

Posted on 2003-11-14
10
689 Views
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?
0
Comment
Question by:Squeebee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 3

Assisted Solution

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

1>This function will help you parse the file
http://us4.php.net/manual/en/function.file.php
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.

$parseFile=file(theFileToBeParsed);

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

for($line=0;$line<sizeof($parseFile);$line++){
$string=$parseFile[$line];
if(!(stristr($sting,"<h3>"))){
  $markStr=addslashes(substr(stristr($sting,"<h3>"),0,strpos($string,"</h3>")));
      //0 may need to be changed to 3 on the above line
      //addslashes used in case of ' or " in the string
  $tocMark[]=$markStr;
  $newString="<h3><a name=\"".$markStr."\">".$markStr."</a></h3>";
  $string=$newString;
}//of if
$parseFile[$line]=addslashes($sting);
}//of for loop

3>To generate the file TOC, you will need to have a loop
  for($tocLine=0;$tocLine<sizeof($tocMark);$tocLine++){
    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.
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9751069
Oh and to Get the file back out properly you can either make the line:
$parseFile[$line]=addslashes($sting);
into
echo($sting) or echo(addslashes($sting) OR once out of the loop you should be able to do:
  $output=implode($parseFile);
  echo($output);

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.

Thanks
Red010Knight

0
 
LVL 17

Author Comment

by:Squeebee
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Author Comment

by:Squeebee
ID: 9751119
Sorry, that piece of information changes things I suppose.
0
 
LVL 3

Expert Comment

by:red010knight
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

$tocString="";
$bodyString="";
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
0
 
LVL 3

Expert Comment

by:red010knight
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

Red010Knight
0
 
LVL 17

Author Comment

by:Squeebee
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.
0
 
LVL 11

Accepted Solution

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

<?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];
}
?>  
0
 
LVL 17

Author Comment

by:Squeebee
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.
0
 
LVL 17

Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

691 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