我有以下字符串
$string = '<div id="promo_MLTEST"><h1>Test Promo Add</h1><p>This should add a new promo</p><ul><li>Test List1</li><li>Test List2</li></ul></div>test product 1'
我正在尝试使用 DOMDocument 删除 ID 为 promo_MLTEST 的 div 并离开
test product 1
我试过以下
$doc = new DOMDocument();
$doc->loadHTML($string);
$elements = $doc->getElementById('promo_MLTEST');
while($span = $elements->item(0)) {
$span->parentNode->removeChild($span);
}
echo $doc->saveHTML();
但我收到以下错误
Fatal error: Call to a member function item() on a non-object in...
引用该行
while($span = $elements->item(0)) {
有人可以帮忙吗?
编辑
我也试过
$divMeta = $doc->getElementById('promo_MLTEST');
$divMeta->parentNode->removeChild($divMeta);
echo $sContent = $divMeta->saveHTML();
这给
Fatal error: Call to a member function removeChild() on a non-object in