Example #1
0
    /**
     * This test functions shows an example of SmartDOMDocument in action.
     * A sample HTML fragment is loaded.
     * Then, the first image in the document is cut out and saved separately.
     * It also shows that Russian characters are parsed correctly.
     *
     */
    public static function testHTML()
    {
        $content = <<<CONTENT
<div class='class1'>
  <img src='http://www.google.com/favicon.ico' />
  Some Text
  <p>русский</p>
</div>
CONTENT;
        print "Before removing the image, the content is: \n" . htmlspecialchars($content) . "<br>\n";
        $content_doc = new SmartDOMDocument();
        $content_doc->loadHTML($content);
        try {
            $first_image = $content_doc->getElementsByTagName("img")->item(0);
            if ($first_image) {
                $first_image->parentNode->removeChild($first_image);
                $content = $content_doc->saveHTMLExact();
                $image_doc = new SmartDOMDocument();
                $image_doc->appendChild($image_doc->importNode($first_image, true));
                $image = $image_doc->saveHTMLExact();
            }
        } catch (Exception $e) {
        }
        print "After removing the image, the content is: \n" . htmlspecialchars($content) . "<br>\n";
        print "The image is: \n" . htmlspecialchars($image);
    }