[PHP]Générer un sommaire automatique
Résolu
_juana_
Messages postés
2
Date d'inscription
Statut
Membre
Dernière intervention
-
Keuronde -
Keuronde -
A voir également:
- [PHP]Générer un sommaire automatique
- Sommaire automatique word - Guide
- Réponse automatique thunderbird - Guide
- Recherche automatique des chaînes ne fonctionne pas - Guide
- Logiciel de sauvegarde automatique gratuit - Guide
- Comment générer un qr code - Guide
4 réponses
Expressions rationnelles compatibles Perl en PHP:
https://www.php.net/manual/fr/ref.pcre.php
l'instruction preg_match permet de faire ce genre de manipulation:
https://www.php.net/manual/fr/function.preg-match.php
https://www.php.net/manual/fr/ref.pcre.php
l'instruction preg_match permet de faire ce genre de manipulation:
https://www.php.net/manual/fr/function.preg-match.php
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";
}
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";
}