本文整理汇总了PHP中DOMDocument::saveHTML方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::saveHTML方法的具体用法?PHP DOMDocument::saveHTML怎么用?PHP DOMDocument::saveHTML使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMDocument
的用法示例。
在下文中一共展示了DOMDocument::saveHTML方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: trimFromHTMLString
/**
* @test
*/
public function trimFromHTMLString()
{
$helper = new DOMHelper();
$directory = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'htmlData' . DIRECTORY_SEPARATOR;
$dom = new DOMDocument();
$input = file_get_contents($directory . 'trimAfterString_input_1.html');
// Si le marqueur n'existe pas, le texte est renvoyé intact
$expected = str_replace("\n", "", $input);
$actual = str_replace("\n", "", $helper->trimFromHTMLString($input, "{{XXXXXX}}"));
$this->assertEquals(preg_replace('/\\s+/', '', $expected), preg_replace('/\\s+/', '', $actual));
// Suppression simple
$htmlHead = '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>***</title></head><body>';
$htmlFoot = '</body></html>';
$actual = str_replace("\n", "", $helper->trimFromHTMLString($input, "{{LIRE_LA_SUITE}}"));
$dom->loadHTML($htmlHead . file_get_contents($directory . 'trimAfterString_output_1.html') . $htmlFoot);
$expected = $this->cleanTmpHTML(str_replace("\n", "", $dom->saveHTML()), $htmlHead, $htmlFoot);
$this->assertEquals(preg_replace('/\\s+/', '', $expected), preg_replace('/\\s+/', '', $actual));
// Suppression avec insertion d'un bouton "Lire la suite"
$actual = str_replace("\n", "", $helper->trimFromHTMLString($input, "{{LIRE_LA_SUITE}}", "<button>Lire la suite</button>"));
$dom->loadHTML($htmlHead . file_get_contents($directory . 'trimAfterString_output_2.html') . $htmlFoot);
$expected = $this->cleanTmpHTML(str_replace("\n", "", $dom->saveHTML()), $htmlHead, $htmlFoot);
$this->assertEquals(preg_replace('/\\s+/', '', $expected), preg_replace('/\\s+/', '', $actual));
// Suppression avec insertion d'un texte et d'un bouton "Lire la suite"
$actual = str_replace("\n", "", $helper->trimFromHTMLString($input, "{{LIRE_LA_SUITE}}", "Pour en savoir plus : <button>Lire la suite</button>"));
$dom->loadHTML($htmlHead . file_get_contents($directory . 'trimAfterString_output_3.html') . $htmlFoot);
$expected = $this->cleanTmpHTML(str_replace("\n", "", $dom->saveHTML()), $htmlHead, $htmlFoot);
$this->assertEquals(preg_replace('/\\s+/', '', $expected), preg_replace('/\\s+/', '', $actual));
}
示例2: getMessage
/**
*
* @return string
*/
public function getMessage()
{
if (empty($this->_jobTraining)) {
return '';
}
$this->_dom = new DOMDocument();
$classMessage = 'alert ';
$divFluid = $this->_dom->createElement('div');
$divMessage = $this->_dom->createElement('div');
$divFluid->setAttribute('class', 'row-fluid');
if ($this->_jobTraining->status != 1) {
$classMessage .= 'alert-error';
$iconClass = 'icon-remove-sign';
$alertText = ' Atensaun ';
$message = ' Job Training ' . ($this->_jobTraining->status == 2 ? 'Kansela' : 'Taka') . ' tiha ona, La bele halo Atualizasaun.';
} else {
$iconClass = 'icon-ok-sign';
$classMessage .= 'alert-success';
$alertText = '';
$message = ' Job Training Loke, então bele halo Atualizasaun.';
}
$divMessage->setAttribute('class', $classMessage);
$strong = $this->_dom->createElement('strong');
$i = $this->_dom->createElement('i');
$i->setAttribute('class', $iconClass);
$strong->appendChild($i);
$strong->appendChild($this->_dom->createTextNode($alertText));
$divMessage->appendChild($strong);
$divMessage->appendChild($this->_dom->createTextNode($message));
$divFluid->appendChild($divMessage);
$this->_dom->appendChild($divFluid);
return $this->_dom->saveHTML();
}
示例3: saveHTMLExact
/**
* @param \DOMDocument $dom
* @param \DOMNode $node
* @return string
*/
protected static function saveHTMLExact(\DOMDocument $dom, \DOMNode $node)
{
if ($dom !== null && defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 50306) {
return $dom->saveHTML($node);
}
return preg_replace(array("/^<!DOCTYPE.*?<body>/si", "#</body>.*</html>\$#si"), '', $dom->saveHTML());
}
示例4: trimMarkup
/**
* @param $elements
* @param $length
*
* @return array
*/
protected function trimMarkup($elements, $length)
{
$markup = '';
$textLength = 0;
foreach ($elements as $element) {
if ($element instanceof DOMText) {
$textLength += strlen($element->nodeValue);
$element->nodeValue = substr($element->nodeValue, 0, $length - $textLength);
if ($textLength >= $length - 30) {
$element->nodeValue .= '...';
}
$markup .= $this->DOMDocument->saveHTML($element);
} elseif ($element->firstChild instanceof DOMText) {
$textLength += strlen($element->firstChild->nodeValue);
$element->firstChild->nodeValue = substr($element->firstChild->nodeValue, 0, $length - $textLength);
if ($textLength >= $length - 30) {
$element->firstChild->nodeValue .= '...';
}
$markup .= $this->DOMDocument->saveHTML($element);
} else {
$trimmed = $this->trimMarkup($element, $length - $textLength);
if ($trimmed['finished']) {
return array('finished' => true, 'markup' => $markup . $trimmed['markup']);
}
$markup .= $trimmed['markup'];
$textLength += $trimmed['length'];
}
if ($textLength >= $length - 30) {
return array('finished' => true, 'markup' => $markup, 'length' => $length);
}
}
return array('finished' => false, 'markup' => $markup, 'length' => $length);
}
示例5: Render
/**
* Render the DOM document
*
* @access public
* @return string
*/
public function Render() : string
{
/* ------------------------------------------------------------------------------------------------------
RETURN
------------------------------------------------------------------------------------------------------ */
return $this->DOMDocument->saveHTML();
}
示例6: parseHTML
protected function parseHTML($body)
{
if (strpos($body, '<div class="description">未找到相關資料</div>')) {
return array();
}
$doc = new DOMDocument();
$full_body = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></html><body>' . $body . '</body></html>';
@$doc->loadHTML($full_body);
$result_dom = $doc->getElementById('hiddenresult');
if (is_null($result_dom)) {
var_dump($body);
throw new Exception('找不到 div#hiddenresult');
}
$count = 0;
$results = array();
foreach ($result_dom->childNodes as $div_result_dom) {
if ('#text' == $div_result_dom->nodeName) {
continue;
}
if ('div' != $div_result_dom->nodeName) {
throw new Exception("div#hiddenresult 下面只會是 div.result");
}
foreach ($div_result_dom->childNodes as $tr_dom) {
if ('tr' == $tr_dom->nodeName) {
$count++;
$results[$count] = new StdClass();
$results[$count]->content = $doc->saveHTML($tr_dom);
} elseif ('script' == $tr_dom->nodeName) {
$results[$count]->script = $doc->saveHTML($tr_dom);
}
}
}
return array_values($results);
}
示例7: buildMenu
public function buildMenu($contents)
{
$this->dom = new \DOMDocument();
$menu = $this->createMenu($contents);
$this->dom->appendChild($menu);
$result = $this->dom->saveHTML();
return $result;
}
示例8: transform
public function transform()
{
E()->getResponse()->setHeader('Content-Type', 'text/javascript; charset=utf-8');
$component = $this->document->getElementById('result');
if (!$component) {
throw new SystemException('ERR_BAD_OPERATION_RESULT', SystemException::ERR_CRITICAL, $this->document->saveHTML());
}
return $component->nodeValue;
}
示例9: modChrome_material_card
function modChrome_material_card($module, &$params, &$attribs)
{
if (!empty($module->content)) {
$extractImage = ArrayHelper::getValue($attribs, 'extractimage', true);
$titleImage = '';
if ($extractImage) {
$doc = new DOMDocument();
$doc->loadHTML($module->content);
$images = $doc->getElementsByTagName('img');
$i = 0;
foreach ($images as $image) {
if ($i > 0) {
continue;
}
$titleImage = $doc->saveHTML($image);
$image->parentNode->removeChild($image);
$i++;
}
$module->content = $doc->saveHTML();
}
?>
<div class="col-grow-vertical <?php
echo htmlspecialchars($params->get('moduleclass_sfx')) . ' ' . ArrayHelper::getValue($attribs, 'col');
?>
">
<div class="card">
<?php
if ($titleImage !== '') {
?>
<div class="card-image">
<?php
echo $titleImage;
?>
</div>
<?php
}
?>
<div class="card-content">
<?php
if ($module->showtitle != 0) {
?>
<span class="card-title"><?php
echo $module->title;
?>
</span>
<?php
}
?>
<?php
echo $module->content;
?>
</div>
</div>
</div>
<?php
}
}
示例10: toHtml
/**
* Convert document to HTML.
*
* @return string
*/
public function toHtml()
{
$html = null;
$nodes_list = $this->document->getElementsByTagName('body')->item(0)->childNodes;
foreach ($nodes_list as $item) {
$html .= $this->document->saveHTML($item);
}
return $html;
}
示例11: getNewBoard
public static function getNewBoard($bid)
{
$url = "https://forum.blockland.us/index.php?board={$bid}.0";
// we only need to check one page
// I doubt there'll be that many updates in a minute
$html = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$tables = $dom->getElementsByTagName("table");
$header = $tables->item(0);
$welcome = $header->getElementsByTagName("td")->item(1);
date_default_timezone_set('US/Eastern');
$text = $dom->saveHTML($welcome);
$lines = explode("<br>", $text);
$words = explode(" ", $lines[2]);
$date = str_replace($words[2] . " ", "", $lines[2]);
$forumTime = strtotime(strip_tags($date));
$table = null;
foreach ($tables as $tab) {
if ($tab->getAttribute("class") == "bordercolor") {
$table = $tab;
}
}
$nlist = $table->getElementsByTagName("tr");
foreach ($nlist as $index => $tr) {
if ($index == 0) {
continue;
}
$tds = $tr->getElementsByTagName("td");
$name = "";
$topic = "";
foreach ($tds as $idx => $td) {
if ($idx == 1) {
$u = $td->getElementsByTagName("a")->item(0);
$name = $u->textContent;
$topic = str_replace("https://forum.blockland.us/index.php?topic=", "", $u->getAttribute("href"));
} else {
if ($idx == 5) {
$text = $dom->saveHTML($td);
$lines = explode("<br>", $text);
$date = strip_tags($lines[0]);
$date = str_replace("Today at", date("F j,", $forumTime), $date);
$time = strtotime($date);
$author = trim(strip_tags(substr($lines[1], 3, strlen($lines[1]))));
if ($time > $forumTime - 60) {
echo $topic . "\n";
//NotificationManager::sendPushNotification("9789", "Forum Post", $text = "<color:3333ff><font:verdana bold:13>$author<font:verdana:13><color:000000> posted a reply in <font:verdana bold:13>$name", "newspaper", "", 0);
}
}
}
}
}
}
示例12: stripStyles
function stripStyles($partBodyData)
{
$data = strtr($partBodyData, array('-' => '+', '_' => '/'));
$data = base64_decode($data);
$doc = new DOMDocument();
@$doc->loadHTML($data);
foreach ($doc->getElementsByTagName("style") as $style) {
$style->parentNode->removeChild($style);
$doc->saveHTML();
}
$encodedDom = base64_encode($doc->saveHTML());
$encodedDom = strtr($encodedDom, array('+' => '+', '/' => '_'));
return $encodedDom;
}
示例13: getItemData
public function getItemData($itemId)
{
$xml = $this->getItem($itemId);
$doc = new DOMDocument();
$desdoc = new DOMDocument();
$doc->loadXML($xml);
$desdoc->loadHTML($doc->getElementsByTagName("Description")->item(0)->nodeValue);
// GET DESCRIPTION DIV
$divs = $desdoc->getElementsByTagName("div");
foreach ($divs as $div) {
if ($div->attributes->getNamedItem("class")->nodeValue != "des") {
continue;
}
$des = $div;
break;
}
$descr_str = $desdoc->saveHTML($des);
////////////////////////
// GET STYLES
$style_str = "";
$styles = $desdoc->getElementsByTagName("style");
foreach ($styles as $style) {
$style_str .= $desdoc->saveHTML($style);
}
////////////////////////
// GET Scripts
$script_str = "";
$scripts = $desdoc->getElementsByTagName("script");
foreach ($scripts as $script) {
$script_str .= $desdoc->saveHTML($script);
}
////////////////////////
$item = new Item();
$item->description = $script_str . " " . $style_str . " " . $descr_str;
$item->setSku($doc->getElementsByTagName("SKU")->item(0)->nodeValue);
$item->price = $doc->getElementsByTagName("CurrentPrice")->item(0)->nodeValue;
$item->currency = $doc->getElementsByTagName("CurrentPrice")->item(0)->attributes->getNamedItem("currencyID")->nodeValue;
$item->quantity = $doc->getElementsByTagName("Quantity")->item(0)->nodeValue;
$item->categoryId = $doc->getElementsByTagName("CategoryID")->item(0)->nodeValue;
$item->categoryName = $doc->getElementsByTagName("CategoryName")->item(0)->nodeValue;
$pictures = $doc->getElementsByTagName("PictureURL");
$item->pictures = array();
foreach ($pictures as $pic) {
array_push($item->pictures, $pic->nodeValue);
}
$item->name = $doc->getElementsByTagName("Title")->item(0)->nodeValue;
return $item;
}
示例14: returnHtml
/**
* Returns the full HTML text in the original charset.
*
* @param array $opts Additional options: (since 2.1.0)
* - charset: (string) Return using this charset. If set but empty, will
* return as currently stored in the DOM object.
* - metacharset: (boolean) If true, will add a META tag containing the
* charset information.
*
* @return string HTML text.
*/
public function returnHtml(array $opts = array())
{
$curr_charset = $this->getCharset();
if (strcasecmp($curr_charset, 'US-ASCII') === 0) {
$curr_charset = 'UTF-8';
}
$charset = array_key_exists('charset', $opts) ? empty($opts['charset']) ? $curr_charset : $opts['charset'] : $this->_origCharset;
if (empty($opts['metacharset'])) {
$text = $this->dom->saveHTML();
} else {
/* Add placeholder for META tag. Can't add charset yet because DOM
* extension will alter output if it exists. */
$meta = $this->dom->createElement('meta');
$meta->setAttribute('http-equiv', 'content-type');
$meta->setAttribute('horde_dom_html_charset', '');
$head = $this->getHead();
$head->insertBefore($meta, $head->firstChild);
$text = str_replace('horde_dom_html_charset=""', 'content="text/html; charset=' . $charset . '"', $this->dom->saveHTML());
$head->removeChild($meta);
}
if (strcasecmp($curr_charset, $charset) !== 0) {
$text = Horde_String::convertCharset($text, $curr_charset, $charset);
}
if (!$this->_xmlencoding || ($pos = strpos($text, $this->_xmlencoding)) === false) {
return $text;
}
return substr_replace($text, '', $pos, strlen($this->_xmlencoding));
}
示例15: parseHtml
function parseHtml()
{
//$this->_html = str_replace("<!DOCTYPE html>", "", $this->_html);
/*
$p = xml_parser_create();
xml_parse_into_struct($p, $this->_html, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
pr($index);
echo "\nVals array\n";
pr($vals);
die;
*/
/*
$this->_html = str_replace("<!DOCTYPE html>", "", $this->_html);
$__data = new SimpleXMLElement($this->_html);
pr($__data); die;
*/
$dom = new DOMDocument();
//echo htmlentities($this->_html); die;
libxml_use_internal_errors(true);
//$this->_html = str_replace("<!DOCTYPE html>", "", $this->_html);
//$this->_html = str_replace("</h1>", "", $this->_html);
//echo htmlentities($this->_html); die;
//echo htmlentities($this->_html); die;
$dom->loadHTML($this->_html);
foreach ($dom->getElementsByTagName('img') as $node) {
$array[] = $dom->saveHTML($node);
}
//pr($array);
//die;
}