_juana_
2Messages postés
21 mars 2007Date d'inscription
21 mars 2007 à 20:18
Merci.
C'est pas exactement ce que je cherchais mais finalement j'ai bidouiller un truc avec preg_split :
function makeTableOfContents($file) {
echo "\t<b>Table of contents</b>\n";
echo "\t<ol>\n";
$tableau = file($file);
$pattern='<h[0-9]*>.*</h[0-9]*>';
$oldN = 3;
// For each line of the HTML file, gets <hn>title</hn>
while(list($cle,$str) = each($tableau)) {
// If line contains pattern
if (eregi($pattern, $str)) {
$chars = preg_split('/<h/', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
// Get n
$fullN = $chars[1][0];
$n = substr($fullN, 0, 1); // only works for digit titles (from h0 to h9)
// Get title
$fullTitle = substr($fullN, 2);
$closeTag = '/</';//.$n.'>/';
$titleSplit = preg_split($closeTag, $fullTitle, -1, PREG_SPLIT_OFFSET_CAPTURE);
$title = substr($fullTitle, 0, ($titleSplit[1][1]-1));
// Display list
if($n >= 3) { // Don't display titles h1 and h2
if ($n > $oldN) {
echo "\t<ol>\n";
}
else if ($n < $oldN) {
echo "\t</ol>\n\t</li>\n";
}
echo "\t<li>".$title."\n";
$oldN = $n;
}
}
}
fclose($file);
echo "\t</ol>\n";
}