Image always after 2nd paragraph of a text

Asked

Viewed 36 times

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.

  • Show what you’ve done

  • A different image or always the same image?

  • 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>

1 answer

0

ideone

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

You are not signed in. Login or sign up in order to post.