Alttaki kodlar işinizi görür kendinize göre düzenliyebilirsiniz.
function SeoYap($baslik)
{
$onceki = array('Ç', 'Ş', 'Ğ', 'Ü', 'İ', 'Ö', 'ç', 'ş', 'ğ', 'ü', 'ö', 'ı', ' ', '.');
$sonraki = array('c', 's', 'g', 'u', 'i', 'o', 'c', 's', 'g', 'u', 'o', 'i', '-', '');
$yeni = str_replace($onceki, $sonraki, $baslik);
$yeni = preg_replace("@[^A-Za-z0-9\.\-_]@i", '', $yeni);
$yeni = strtolower($yeni);
return $yeni;
}
function toc($html)
{
preg_match_all('@<p><strong>(.*?)</strong></p>@si', $html, $matches);
if (empty($matches[0])) {
return ["html" => $html, "index" => ""];
} else {
$index = '<h3>Hızlı Bakış</h3><ol class="toc_list">';
$prev = 2;
foreach ($matches[0] as $i => $match) {
$curr = $matches[1][$i];
$text = strip_tags($matches[1][$i]);
$slug = SeoYap($text);
$anchor = '<a name="' . $slug . '">' . $text . '</a>';
$html = str_replace($text, $anchor, $html);
$prev <= $curr ?: $index .= str_repeat('</ol>', ($prev - $curr));
$prev >= $curr ?: $index .= "<ol>";
$index .= '<li class="h6"><a href="#' . $slug . '">' . $text . '</a></li>';
$prev = $curr;
}
$index .= "</ol>";
return ["html" => $html, "index" => $index];
}
}
$html = "<p><strong>Benim Site Örnek Başlık</strong></p><p>makale ve başlık içeriği</p><p><strong>Benim Site Örnek Başlık 1</strong></p><p>makale ve başlık içeriği 1</p>";
echo toc($html)['index'];
echo toc($html)['html'];Ekran Görüntüsü :