BthnSnt adlı üyeden alıntı:
mesajı görüntüle
PHP str_replace Hatam Nerede?
14
●360
- 14-02-2023, 10:32:51Dediğiniz sonucu vermedi maalesef. @BthnSnt; 'nın dediği gibi bir regex ile deneyeyim.Luadex IT adlı üyeden alıntı: mesajı görüntüle
- 14-02-2023, 11:05:34
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"; - 14-02-2023, 11:13:58Hocam süpersin ya. Tam mantığını anlatabilir misin?mikropiks adlı üyeden alıntı: mesajı görüntüle
- 14-02-2023, 11:31:28iltu33 adlı üyeden alıntı: mesajı görüntüle
preg_match_all('/<h[1-6]>(.*?)<\/h[1-6]>/', $content, $cikti);Bu kısmında $cikti match olan tüm sonucu döndürmekte. Örneğin h1 buldu ise $cikti[0] da "<h1>h1_icerik</h1>", $cikti[1] de ise "h1_icerik" şeklinde aynı keye bağlı array döndürmekte.
Görsel olarak aşağıda ki gibi
Array ( [0] => Array ( [0] => <h1>h1_icerik </h1> [1] => <h2>h2</h2> [2] => <h3>h3</h3> [3] => <h4>h4</h4> [4] => <h5>h5</h5> [5] => <h6>h6</h6> ) [1] => Array ( [0] => h1_icerik [1] => h2 [2] => h3 [3] => h4 [4] => h5 [5] => h6 ) )
Sonrasında foreach e $cikti[0] ı sokuyoruz 'key' ile. Döngüde o anki key verinin hem ham halini hem de içerik halini bulmamız için lüzumlu.
Döngüde elimizde key dahil 3 veri oluyor
$cikti[0][$key] = <h1>h1_icerik</h1>
$cikti[1][$key] = h1_icerik
Geri kalan içlem replace ile değişikliği yapmak. "<h1>h1_icerik</h1>($cikti[0][$key] ) içeriği bulup <h1 id="h1_icerik($cikti[1][$key] )">h1_icerik($cikti[1][$key] )</h1>" ile değiştirmek.