IMHO it would be best to deal with this using DOMDocument. As always this is more complicated than using replace/regexes - but usually it's worth the effort as it handles the content as HTML rather than just plain text.
The main code for being able to process document fragments is heavily based on https://stackoverflow.com/a/29499398/1213708, all I have done is added the ability to refer to the paragraphs as you are after.
The p tags part is just the
$pTags = $doc->getElementsByTagName("p");
$pTags[1]->textContent = "cambio";
so first is to get a list of the p tags - you now have an array which you can set as in the second line of code.
$result_information = "<p>parrafo 1</p> <p>parrafo 2</p> <p>parrafo 3</p>";
$doc = new DOMDocument();
$doc->loadHTML("<div>$result_information</div>");
$pTags = $doc->getElementsByTagName("p");
$pTags[1]->textContent = "cambio";
$container = $doc->getElementsByTagName('div')->item(0);
$container = $container->parentNode->removeChild($container);
while ($doc->firstChild) {
$doc->removeChild($doc->firstChild);
}
while ($container->firstChild ) {
$doc->appendChild($container->firstChild);
}
echo $doc->saveHTML();
` or use an html parsing library to identify and replace the portions you need. You could also hack something together with this one: https://stackoverflow.com/questions/5696412/how-to-get-a-substring-between-two-strings-in-php + `str_replace()`
– TCooper Dec 06 '19 at 20:09