读取 simpleDomObject 时出错
我有以下模板文件,名为“test.html”,
<div class='title'>TEST</div>
并且我有以下 PHP 代码:
<?
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "test.html" );
echo $dom->outertext;
?>
到目前为止一切顺利,这显示了文件 test.html。但是,当我尝试更改某些内容时,出现错误:
<?
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "test.html" );
$dom->find('.title')->innertext = "changed";
echo $dom->outertext;
?>
警告:尝试在第 4 行的 E:\internet\test.php 中分配非对象的属性。我相信我完全按照手册进行操作。这里出了什么问题?显然 $dom->find('.title') 没有返回有效元素,但问题是:为什么?它应该找到DIV吗?
I have the following template file, named 'test.html'
<div class='title'>TEST</div>
And I have the following PHP code:
<?
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "test.html" );
echo $dom->outertext;
?>
So far so good, this displays the file test.html. But when I try to change something I get an error:
<?
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "test.html" );
$dom->find('.title')->innertext = "changed";
echo $dom->outertext;
?>
Warning: Attempt to assign property of non-object in E:\internet\test.php on line 4. Though I do believe I'm exactly following the manual. What is going wrong here? Obviously $dom->find('.title') didn't return a valid element, but the question is: why? It should find the DIV?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一:
您显然错过了找到的元素的索引,因此没有属性
find()->innertext
此处修复了代码:
第二:
我不建议您使用
Simple Html DOM
库,因为它是旧的并且不是实际的看一下
QueryPath
库,它做着同样的事情,而且状况更好。http://querypath.org/
First:
You obviously missed index for found elements, so there isn't property
find()->innertext
repaired code here:
Second:
I wouldn't recommend you to use
Simple Html DOM
library, beacuse it's old and not actualTake a look at
QueryPath
library, which is doing the same and is in better condition.http://querypath.org/