php替换字符串中的id元素内容

php替换字符串中的id元素内容,php,Php,有点卡住了,是否可以使用PHP更新/更改html元素内容。 因此,根据id名称,仅替换下面字符串中的“Hello World” $html = '<h1 id="item" class="abc" data="efg">Hello World</h1>'; $html='Hello World'; 也许使用preg_replace,只是想将所有其他内容保留在那里,例如类、数据等 $html = preg_replace('<h1 id="item">???

有点卡住了,是否可以使用PHP更新/更改html元素内容。 因此,根据id名称,仅替换下面字符串中的“Hello World”

$html = '<h1 id="item" class="abc" data="efg">Hello World</h1>';
$html='Hello World';
也许使用preg_replace,只是想将所有其他内容保留在那里,例如类、数据等

$html = preg_replace('<h1 id="item">????</h1>', 'New Content', $html);
$html=preg_replace(“?”、“新内容”、$html);
谢谢

可能在下面代码会有帮助
$html='helloworld';
$elementId=“项目”;
$newString=“替换世界”;
$dom=新的DOMDocument();
$dom->loadHTML($html);
$belement=$dom->getElementById($elementId”);
$oldString=$belement->nodeValue;
$newHTML=str_replace(“$oldString”、“$newString”、“$html”);
echo$newHTML;
注意:请使用原始值更改$elementId和$newString

这是否回答了您的问题?或者这个:?
!=<代码>谢谢大家-谢谢!答案正是我想要的。
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