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.
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.
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;