getting an error reading simpleDomObject

2019-09-01 16:35发布

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?

1条回答
聊天终结者
2楼-- · 2019-09-01 17:11

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/

查看更多
登录 后发表回答