读取 simpleDomObject 时出错

发布于 2024-11-17 19:42:00 字数 685 浏览 1 评论 0原文

我有以下模板文件,名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-11-24 19:42:00

第一:

您显然错过了找到的元素的索引,因此没有属性 find()->innertext
此处修复了代码:

<?php
error_reporting(E_ALL);
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "index.html" );
$dom->find('.title',0)->innertext = "changed";
echo $dom->outertext;

第二:

不建议您使用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:

<?php
error_reporting(E_ALL);
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "index.html" );
$dom->find('.title',0)->innertext = "changed";
echo $dom->outertext;

Second:

I wouldn't recommend you to use Simple Html DOM library, beacuse it's old and not actual
Take a look at QueryPath library, which is doing the same and is in better condition.

http://querypath.org/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文