If you don't know what's inside the
tag, it's possible there is another
tag in there e.g.
somethingsomething else
And so a generic string replace function won't do the job.
A more robust solution is to treat the string as XML and manipulate it with DOMDocument
. Admittedly this only works if the string is valid as XML, but I still think it's a better solution than a string replace.
$string = "i don't know what is here";
$replacement = "replacement";
$doc = new DOMDocument();
$doc->loadXML($str1);
$node = $doc->getElementsByTagName('tag')->item(0);
$newNode = $doc->createElement("tag", $replacement);
$node->parentNode->replaceChild($newNode, $node);
echo $str1 = $doc->saveHTML($node); //output: replacement