好吧,这个文件的目标是从一些 xml 提要中读取标题,并修改我在本地的文件。我已经让 simplexml 开始工作,但是当我添加简单的 html dom 时,一切都变得很糟糕。当我运行该文件时,我不断得到:
Creating default object from empty value
我看过其他似乎对其他人有效的堆栈溢出解决方案,例如使用:
$title = new stdClass;
所以我不太确定问题出在哪里。提前感谢您的帮助。
require("simple_html_dom.php");
$html = file_get_html('../index.html');
$xml = simplexml_load_file("https://www.figurescreed.moe/feed/") or
die("Error: Cannot create object");;
for ($x = 0; $x < 3; $x++) {
$title = new stdClass;
$element = new stdClass;
$title = $xml->channel->item[$x]->title;
$element = $html->find('div[id=title]');
$element[$x]->innertext = $title;
echo $html;
}
?>
最佳答案
尝试检查 $element 是一个数组,在这一行之后至少有 3 个元素:
$element = $html->find('div[id=title]');
你可以检查一下
echo '<pre>' . htmlspecialchars (print_r ($element, true)) . '</pre>';
或者如果您从命令行运行它:
echo print_r ($element, true);
如果其中没有 3 个元素,您可能会在这一行收到错误:
$element[$x]->innertext = $title;
因为$element[$x] 引用了一个不存在的数组元素。
可能您的 html 文件中没有 3 个项目匹配 div[id=title]
这两行是不必要的,因为你之后会立即覆盖它们:
$title = new stdClass;
$element = new stdClass;
关于php - 从空值创建默认对象 - 简单的 HTML DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46875907/