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?
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 actual Take a look atQueryPath
library, which is doing the same and is in better condition.http://querypath.org/