function seo($x){
return $x;
}
$content = '<h1>h1</h1><h2>h2</h2><h3>h3</h3><h4>h4</h4><h5>h5</h5><h6>h6</h6>';
print_r($content);
echo "\n\n---\n\n";
preg_match_all('/<h[1-6]>(.*?)<\/h[1-6]>/', $content, $cikti);
foreach ($cikti[0] as $key => $element) {
$title = $cikti[1][$key];
$content = str_replace($element, '<h1 id="' . seo($title) . '">' . $title . '</h1>', $content);
$content = str_replace($element, '<h2 id="' . seo($title) . '">' . $title . '</h2>', $content);
$content = str_replace($element, '<h3 id="' . seo($title) . '">' . $title . '</h3>', $content);
$content = str_replace($element, '<h4 id="' . seo($title) . '">' . $title . '</h4>', $content);
$content = str_replace($element, '<h5 id="' . seo($title) . '">' . $title . '</h5>', $content);
$content = str_replace($element, '<h6 id="' . seo($title) . '">' . $title . '</h6>', $content);
}
print_r($content);
echo "\n\n---\n\n";Hocam bu kodlama ile aşağıda ki çıktıyı veriyor
<h1>h1</h1><h2>h2</h2><h3>h3</h3><h4>h4</h4><h5>h5</h5><h6>h6</h6>
---
<h1 id="h1">h1</h1><h1 id="h2">h2</h1><h1 id="h3">h3</h1><h1 id="h4">h4</h1><h1 id="h5">h5</h1><h1 id="h6">h6</h1>
---
replace search kısmını $cikti[0] dan alıyoruz. Replace edilecek kısmı bulup $cikti[1] den preg match den gelen başlığı alıyoruz.
Bu da PREG_SET_ORDER lı şekilde kullanımı daha temiz gibi
function seo($x){
return $x;
}
$content = '<h1>h1_icerik</h1><h2>h2</h2><h3>h3</h3><h4>h4</h4><h5>h5</h5><h6>h6</h6>';
print_r($content);
echo "\n\n---\n\n";
preg_match_all('/<h[1-6]>(.*?)<\/h[1-6]>/', $content, $cikti, PREG_SET_ORDER);
print_r($cikti);
foreach ($cikti as $element) {
$search = $element[0]; // <h1>h1_icerik</h1>
$title = $element[1]; // h1_icerik
$content = str_replace($search, '<h1 id="' . seo($title) . '">' . $title . '</h1>', $content);
$content = str_replace($search, '<h2 id="' . seo($title) . '">' . $title . '</h2>', $content);
$content = str_replace($search, '<h3 id="' . seo($title) . '">' . $title . '</h3>', $content);
$content = str_replace($search, '<h4 id="' . seo($title) . '">' . $title . '</h4>', $content);
$content = str_replace($search, '<h5 id="' . seo($title) . '">' . $title . '</h5>', $content);
$content = str_replace($search, '<h6 id="' . seo($title) . '">' . $title . '</h6>', $content);
}
print_r($content);
echo "\n\n---\n\n";