Skip to content

Instantly share code, notes, and snippets.

@CB9TOIIIA
Created May 5, 2018 11:01
Show Gist options
  • Save CB9TOIIIA/545d465a2ea2e7b4f97ca74a92bc2ada to your computer and use it in GitHub Desktop.
Save CB9TOIIIA/545d465a2ea2e7b4f97ca74a92bc2ada to your computer and use it in GitHub Desktop.
Содержание
$result = array();
$nav_cont = '';
#########################################
$smatches = array();
preg_match_all("/\<[hH]([1-6])\>(.*?)\<\/[hH][1-6]\>/s",$this->item->text,$smatches);
$content = $this->item->text;
################################
$quantityanchor=0;//Счетчик для якоря
$soderjanie='<div id="navigator"><span>Содержание:</span><ol>';
$matches = array();
for ($i=0;$i<count($smatches[0]);$i++){
if ($i<>0 and $smatches[1][$i]>$smatches[1][$i-1]){
$soderjanie.='<ol class="header'.$smatches[1][$i].'">';
}elseif ($i<>0 and $smatches[1][$i]<$smatches[1][$i-1]){
$quantity=$smatches[1][$i-1]-$smatches[1][$i];
for ($j=1;$j<=$quantity;$j++)$soderjanie.="</ol>";
} else {
$soderjanie.="</li>";
}
$quantityanchor++;
$soderjanie.='<li><a href="#title'.($quantityanchor-1).'">'.$smatches[2][$i].'</a>';
#$this->item->text=str_replace($smatches[0][$i],"<h".$smatches[1][$i]." id=\"title".$i."\">".$smatches[2][$i]."</h".$smatches[1][$i].">",$this->item->text);
$smatches[0][$i] = str_replace('?','\?', $smatches[0][$i]);
$smatches[0][$i] = str_replace('(','\(', $smatches[0][$i]);
$smatches[0][$i] = str_replace(')','\)', $smatches[0][$i]);
$this->item->text = preg_replace('|'.$smatches[0][$i].'|isU', "<h".$smatches[1][$i]." id=\"title".$i."\">".$smatches[2][$i]."</h".$smatches[1][$i].">",$this->item->text,1);
}
$soderjanie.="</li></ol></div>";
######################################################
echo $soderjanie;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment