/** * 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: " . htmlspecialchars($content) . "<br>"; $content_doc = new Dom_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 Dom_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: " . htmlspecialchars($content) . "<br>"; print "The image is: " . htmlspecialchars($image); }
public function getHtmlFilePath() { if (!file_exists(Core_Model_Directory::getCacheDirectory(true) . '/' . $this->_getFilename())) { $file = fopen(Core_Model_Directory::getCacheDirectory(true) . '/' . $this->_getFilename(), 'w'); $html_code = mb_convert_encoding(html_entity_decode($this->getText()), 'HTML-ENTITIES', 'UTF-8'); $html_a_target = "_top"; if ($this->getSession()->isOverview) { $html_a_target = "_blank"; } //adding or changing target of the <a> $doc = new Dom_SmartDOMDocument(); $doc->loadHTML($html_code); $links = $doc->getElementsByTagName('a'); foreach ($links as $item) { $item->setAttribute('target', $html_a_target); } $html_code = html_entity_decode($doc->saveHTML(), ENT_QUOTES, "UTF-8"); $html = '<html><head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" name="viewport" /> <meta content="black" name="apple-mobile-web-app-status-bar-style" /> <meta content="IE=8" http-equiv="X-UA-Compatible" /> <style type="text/css"> html, body { margin:0; padding:0; border:none; } html { overflow: scroll; } body { font-size: 15px; width: 100%; height: 100%; overflow: auto; -webkit-user-select : none; -webkit-text-size-adjust : none; -webkit-touch-callout: none; line-height:1; background-color:white; } </style> </head>' . html_entity_decode($html_code) . '</html>'; fputs($file, $html); fclose($file); } return Core_Model_Directory::getCacheDirectory() . '/' . $this->_getFilename(); }