Thursday, February 10, 2011

Simple HTML Dom is nice

Recently, I had to read a web page, make some minor changes to it, and save the results. I started with DOMDocument, but couldn't figure out how to write to a DOMNode.

Then I found Simple HTML DOM, and it's a nice piece of software.

It's as easy as this:
$html = file_get_html($url);

$someNode = $html->find($someXpath);
$someNode->innertext = "Hello, World!";

echo $html->save();
That's it!

5 comments:

  1. How to config using DOMDocument on Yii? I don`t know using that! please help me!

    ReplyDelete
  2. you should use it for parsing html, for example, if you need to request a web page from yii, or if someone posts html to you.

    you can put the simple_html_dom.php file in your components folder. you have to edit it slightly so that the class simple_html_dom {} is the first class on that page (just move it near the top)

    ReplyDelete
  3. Thanks Neil McGuigan!

    I put simple_html_dom.php in extension folder, in a file in commands folder, include_one('/../extension/simple_html_dom.php'). Now I can using HTML DOM on the file in commands folder. thanks for share!

    ReplyDelete
  4. Neil,
    i've done this, but i got this error:
    Fatal error: Call to undefined function file_get_html() in C:\xampp\htdocs\hot\protected\controllers\HotController.php on line 127

    ReplyDelete
  5. Hi Mr Neil McGuigan,
    I've integrated Simple html dom in my Yii project.
    But I got the error: Trying to get property of non-object when i try:
    $img = $e->find('img')->outertext;
    var_dump($img);
    So I always use foreach loop to solve that problem :(.
    Could you help me, thank you so much!

    ReplyDelete