-1

a bit stuck, is it possible to use PHP to update/change html element contents. So replace only 'Hello World' in the string below based off the the id name.

$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';

Perhaps using preg_replace, just want to leave all other content in there, eg. class, data, etc.

$html = preg_replace('<h1 id="item">????</h1>', 'New Content', $html);

Thanks

1 Answers1

2
May be below code will help 

$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';
$elementId = "item";
$newString = "Replace World";

$dom = new DOMDocument();
$dom->loadHTML($html);
$belement = $dom->getElementById("$elementId");
$oldString = $belement->nodeValue;


$newHTML = str_replace("$oldString","$newString","$html");
echo $newHTML;


Note: Please change $elementId and $newString with original values