0
Hello, I would like to put picture always after 2nd paragraph of a text, I thought to replace the 2nd occurrence </p>
and trade it for the image, but I couldn’t do.
0
Hello, I would like to put picture always after 2nd paragraph of a text, I thought to replace the 2nd occurrence </p>
and trade it for the image, but I couldn’t do.
0
Function to find 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;
}
Applying
$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;
Browser other questions tagged php string
You are not signed in. Login or sign up in order to post.
Show what you’ve done
– Papa Charlie
A different image or always the same image?
– Fabri Damazio
actually it is a div that will receive the image and tals, but the same problem is to scan the text find the second paragraph that would be after the second </p> and replace by </p> <div>...</div>
– Rodrigo Nascimento