Image always after 2nd paragraph of a text

0

Hello, I would like to put image always after 2nd paragraph of a text, I thought to replace the 2nd occurrence </p> and exchange it for the image, but I could not do it.

    
asked by anonymous 14.05.2017 / 17:27

1 answer

0

ideone

Function to fetch the second position of the closing tag </p>

function pesquisarStrpos($palheiro, $agulha, $pesquisa) {
    $count = 0;
    $pos = -1;
    do {
        $pos = strpos($palheiro, $agulha, $pos + 1);
        $count++;
    } while ($pos !== false && $count < $pesquisa);
    return $pos;
}

Application

$texto="<p>primeiro paragrafo</p><p>segundo paragrafo</p><p>terceiro paragrafo</p>";

$pos = (pesquisarStrpos($texto, '</p>', 2));

$textoInicio=substr($texto,0,$pos+4);

$textoFim=substr($texto,$pos+4,strlen($texto));

$result=$textoInicio."<div>...</div>".$textoFim;
    
22.05.2017 / 16:18