getting an error reading simpleDomObject

为君一笑 提交于 2019-12-13 04:51:34

问题


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:


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/



来源:https://stackoverflow.com/questions/6519293/getting-an-error-reading-simpledomobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!