本文整理汇总了PHP中DOMDocument::saveHtml方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::saveHtml方法的具体用法?PHP DOMDocument::saveHtml怎么用?PHP DOMDocument::saveHtml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMDocument
的用法示例。
在下文中一共展示了DOMDocument::saveHtml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testRequestToOutputFile
function testRequestToOutputFile()
{
$client = new ProxyClient();
$client->URL = df_absolute_url('tests/test_ProxyClient/test1.html');
$outputFile = tempnam(sys_get_temp_dir(), 'test_ProxyClient');
$client->outputFile = $outputFile;
$client->process();
$this->assertEquals(null, $client->content, 'Content should be written to output file, not saved to variable.');
$expected = file_get_contents('tests/test_ProxyClient/test1.html');
$doc = new DOMDocument();
@$doc->loadHtml($expected);
$expected = $doc->saveHtml();
$actual = file_get_contents($outputFile);
$actual = '';
$fh = fopen($outputFile, 'r');
while (!feof($fh) and trim($line = fgets($fh, 1024))) {
// We skip the headers
}
ob_start();
fpassthru($fh);
fclose($fh);
$actual = ob_get_contents();
ob_end_clean();
unset($doc);
$doc = new DOMDocument();
@$doc->loadHtml($actual);
$actual = $doc->saveHtml();
unset($doc);
$this->assertEquals($expected, $actual);
}
示例2: getOuterHtml
/**
* @param \DOMNode $node
* @return string
*/
public static function getOuterHtml(\DOMNode $node)
{
$domDocument = new \DOMDocument('1.0');
$b = $domDocument->importNode($node->cloneNode(true), true);
$domDocument->appendChild($b);
$html = $domDocument->saveHtml();
$html = StringHelper::safeEncodeStr($html);
return $html;
}
示例3: printScript
/**
* Print script which defined attributes
*
* @access public
* @return void
*/
public function printScript()
{
$domDocument = new DOMDocument();
$domScript = $domDocument->createElement('script');
foreach ($this->scriptAttributes as $key => $value) {
$domAttribute = $domDocument->createAttribute($key);
$domAttribute->value = $value;
$domScript->appendChild($domAttribute);
}
$domDocument->appendChild($domScript);
echo $domDocument->saveHtml();
}
示例4: getEmbedCode
public function getEmbedCode()
{
$dom = new \DOMDocument();
$docSrc = URL::to('docs/embed', $this->slug);
$insertElement = $dom->createElement('div');
$containerElement = $dom->createElement('iframe');
$containerElement->setAttribute('id', '__ogFrame');
$containerElement->setAttribute('width', 300);
$containerElement->setAttribute('height', 500);
$containerElement->setAttribute('src', $docSrc);
$containerElement->setAttribute('frameBorder', 0);
$insertElement->appendChild($containerElement);
return $dom->saveHtml($insertElement);
}
示例5: get
function get($input)
{
libxml_use_internal_errors(true);
libxml_clear_errors();
$doc = new DOMDocument();
$doc->loadHtml($input);
$xpath = new DOMXPath($doc);
$mainElements = $xpath->query("//div[@class='section results']");
$resultArray = array();
foreach ($mainElements as $mainElement) {
$this->parseEntries($doc->saveHtml($mainElement), $resultArray);
}
return $resultArray;
}
示例6: find
public function find($xp)
{
//var_dump($this->dom);
$xpath = new DOMXpath($this->dom);
//$xpath->registerNamespace('html','http://www.w3.org/1999/xhtml');
$eles = $xpath->query($xp);
//var_dump($eles);
if ($eles->length == 0) {
return '';
}
//var_dump($eles);
$ele = $eles->item(0);
$dom = new DOMDocument();
$dom->appendChild($dom->importNode($ele, true));
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$c = $dom->saveHtml();
$c = mb_convert_encoding($c, $this->charset, 'HTML-ENTITIES');
return $c;
}
示例7: merge
public function merge(array $pages, $separator = '')
{
$head = '';
$output = '';
libxml_use_internal_errors(true);
foreach ($pages as $page) {
if (!$head) {
list($head, $body) = preg_split('/<body/i', $page);
}
$document = new \DOMDocument();
$document->loadHTML($page);
$bodyOnlyDocument = new \DOMDocument();
$body = $document->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
$bodyOnlyDocument->appendChild($bodyOnlyDocument->importNode($child, true));
}
$output .= $bodyOnlyDocument->saveHtml() . $separator;
}
return $head . '<body>' . rtrim($output, $separator) . '</body></html>';
}
示例8: testPage
function testPage()
{
$url = DATAFACE_SITE_URL . '/tests/testsites/site1/index.html';
$site = new Dataface_Record('websites', array());
$site->setValues(array('website_url' => df_absolute_url(DATAFACE_SITE_URL . '/tests/testsites/site1/'), 'source_language' => 'en', 'target_language' => 'fr', 'website_name' => 'Site 1 French', 'active' => 1, 'base_path' => DATAFACE_SITE_URL . '/proxies/site1/', 'host' => $_SERVER['HTTP_HOST']));
$site->save();
df_q("delete from site_text_filters where website_id='" . addslashes($site->val('website_id')) . "'");
$server = new ProxyServer();
$server->site = SweteSite::loadSiteById($site->val('website_id'));
$server->SERVER = array('REQUEST_METHOD' => 'get');
$server->URL = df_absolute_url(DATAFACE_SITE_URL . '/proxies/site1/index.html');
$server->buffer = true;
$server->handleRequest();
$doc = new DOMDocument();
$doc->loadHtml(file_get_contents('tests/testsites/site1_output/index.out.html'));
$expected = $doc->saveHtml();
//echo $server->contentBuffer;
$doc2 = new DOMDocument();
$doc2->loadHtml($server->contentBuffer);
$actual = $doc2->saveHtml();
//$this->assertEquals(trim($expected), trim($actual));
// Cancelled this test because WTF!!!! Even if I print the actual output, copy it to the file
// and compare it to itself, it still fails!!!! WTF!!!!
}
示例9: DOMDocument
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->loadHTMLFile("http://docs.codehaus.org/display/GRADLE/Gradle+0.9+Breaking+Changes");
$xpath = new DOMXPath($xmlDoc);
$entries = $xpath->query("//*[@class='wiki-content']");
foreach ($entries as $entry) {
$copyDoc = new DOMDocument();
$copyDoc->appendChild($copyDoc->importNode($entry, true));
echo $copyDoc->saveHtml();
}
示例10: getSafeOutput
/**
* Returns a safe output to the theme
* This includes setting nofollow tags on links, forcing them to open in new windows, and safely encoding the text
* @return string
*/
public function getSafeOutput()
{
$md = new CMarkdownParser();
$dom = new DOMDocument();
$dom->loadHtml('<?xml encoding="UTF-8">' . $md->safeTransform($this->content));
$x = new DOMXPath($dom);
foreach ($x->query('//a') as $node) {
$element = $node->getAttribute('href');
if (isset($element[0]) && $element[0] !== "/") {
$node->setAttribute('rel', 'nofollow');
$node->setAttribute('target', '_blank');
}
}
return $md->safeTransform($dom->saveHtml());
}
示例11: html
/**
* Dumps the internal document into a string using HTML formatting.
*
* @return string
*/
public function html()
{
return trim($this->document->saveHtml());
}
示例12: brasofilo_suSetHtmlElementById
/**
* Find a tag by ID and append/replace content
*
* source: http://stackoverflow.com/a/17661043/1829145
* Thanks to Rodolfo Buaiz (brasofilo)
*
* @param string $oDoc source html (passed by reference!)
* @param string $s html code to insert
* @param string $sId id of the tag to find
* @param string $sHtml
* @param boolean $bAppend append new code?
* @param boolean $bInsert replace existing contents by the new source code?
* @param boolean $bAddToOuter
* @return boolean
*/
function brasofilo_suSetHtmlElementById(&$oDoc, &$s, $sId, $sHtml, $bAppend = false, $bInsert = false, $bAddToOuter = false)
{
if (brasofilo_suIsValidString($s) && brasofilo_suIsValidString($sId)) {
$bCreate = true;
if (is_object($oDoc)) {
if (!$oDoc instanceof DOMDocument) {
return false;
}
$bCreate = false;
}
if ($bCreate) {
$oDoc = new DOMDocument();
}
libxml_use_internal_errors(true);
$oDoc->loadHTML($s);
libxml_use_internal_errors(false);
$oNode = $oDoc->getElementById($sId);
if (is_object($oNode)) {
$bReplaceOuter = !$bAppend && !$bInsert;
$sId = uniqid('NVCMS_SHEBI-');
$aId = array("<!-- {$sId} -->", "<!--{$sId}-->");
if ($bReplaceOuter) {
if (brasofilo_suIsValidString($sHtml)) {
$oNode->parentNode->replaceChild($oDoc->createComment($sId), $oNode);
$s = $oDoc->saveHtml();
$s = str_replace($aId, $sHtml, $oDoc->saveHtml());
} else {
$oNode->parentNode->removeChild($oNode);
$s = $oDoc->saveHtml();
}
return true;
}
$bReplaceInner = $bAppend && $bInsert;
$sThis = null;
if (!$bReplaceInner) {
$sThis = $oDoc->saveHTML($oNode);
$sThis = ($bInsert ? $sHtml : '') . ($bAddToOuter ? $sThis : substr($sThis, strpos($sThis, '>') + 1, -(strlen($oNode->nodeName) + 3))) . ($bAppend ? $sHtml : '');
}
if (!$bReplaceInner && $bAddToOuter) {
$oNode->parentNode->replaceChild($oDoc->createComment($sId), $oNode);
$sId =& $aId;
} else {
$oNode->nodeValue = $sId;
}
$s = str_replace($sId, $bReplaceInner ? $sHtml : $sThis, $oDoc->saveHtml());
return true;
}
}
return false;
}
示例13: populateForms
/**
* Populate values in HTML forms.
*
* @param string $body
* @return string
* @todo fix for radio buttons and dropdown boxes!
*/
protected function populateForms($body)
{
$forms = $this->response->getForms();
// If response object holds no form data, we are done.
if (sizeof($forms) == 0) {
return $body;
}
$dom = new \DOMDocument();
$dom->loadHtml($body);
$query = new \DOMXPath($dom);
foreach ($forms as $form) {
foreach ($this->response->getFormValues($form) as $field => $value) {
$nodes = $query->evaluate("//form[@name='" . $form . "']//*[@name='" . $field . "']");
if ($nodes->length == 0) {
throw new Exception('No field ' . $field . ' in form ' . $form);
}
$node = $nodes->item(0);
switch ($node->nodeName) {
case 'input':
if ($node->getAttribute('type') == 'checkbox') {
if ($value != 0) {
$node->setAttribute('checked', true);
}
} else {
$node->setAttribute('value', $value);
}
break;
case 'textarea':
$node->firstChild->nodeValue = $value;
break;
case 'select':
foreach ($node->childNodes as $child) {
if ($child->nodeValue == $value) {
$child->setAttribute('selected', 'selected');
}
}
break;
default:
throw new Exception('Unknown tag ' . $nodes->item(0)->nodeName . ' in form ' . $form);
// @todo DEV only.
}
}
}
/*
// Patch in field error messages.
foreach ($this->response->getFieldErrors($form) as $field => $errorMessages) {
foreach ($errorMessages as $errorMessage) {
$nodes = $query->evaluate("//form[@name='" . $form . "']//*[@name='" . $field . "']");
if ($nodes->length == 0) {
throw new Exception('No field ' . $field . ' in form ' . $form);
}
$node = $nodes->item(0);
$message = new \DOMElement('div', $errorMessage->getMessage());
//$node->parentNode->appendChild($message);
$node->nextSibling->insertBefore($message);
}
}
*/
return $dom->saveHtml();
}
示例14: DOMDocument
<?php
$dom = new DOMDocument();
//$dom = new DOMDocument('1.0', 'utf-8');
$head = $dom->createElement('head');
$title = $dom->createElement('title');
$node = $dom->createTextNode('Hello, World!');
// Solution
$title->appendChild($node);
$head->appendChild($title);
// More
$attr = $dom->createAttribute('id');
$attr->value = 'my-title';
$title->appendChild($attr);
$title->setAttributeNode(new DOMAttr('data-charset', 'utf-8'));
// Render
$dom->appendChild($head);
var_dump($dom->saveXML(), $dom->saveXML($title), $dom->saveHtml());
示例15: extractInteractions
/**
* Extract all interaction by find interaction node & relative choices
* Find right answer & resolve identifier to choice name
* Output example of item interactions:
* array (
* [...],
* array(
* "id" => "56e7d1397ad57",
* "type" => "Match",
* "choices" => array (
* "M" => "Mouse",
* "S" => "Soda",
* "W" => "Wheel",
* "D" => "DarthVader",
* "A" => "Astronaut",
* "C" => "Computer",
* "P" => "Plane",
* "N" => "Number",
* ),
* "responses" => array (
* 0 => "M C"
* ),
* "responseIdentifier" => "RESPONSE"
* )
* )
*
* @return $this
*/
protected function extractInteractions()
{
$elements = ['Choice' => ['domInteraction' => 'choiceInteraction', 'xpathChoice' => './/qti:simpleChoice'], 'Order' => ['domInteraction' => 'orderInteraction', 'xpathChoice' => './/qti:simpleChoice'], 'Match' => ['domInteraction' => 'matchInteraction', 'xpathChoice' => './/qti:simpleAssociableChoice'], 'Associate' => ['domInteraction' => 'associateInteraction', 'xpathChoice' => './/qti:simpleAssociableChoice'], 'Gap Match' => ['domInteraction' => 'gapMatchInteraction', 'xpathChoice' => './/qti:gapText'], 'Hot text' => ['domInteraction' => 'hottextInteraction', 'xpathChoice' => './/qti:hottext'], 'Inline choice' => ['domInteraction' => 'inlineChoiceInteraction', 'xpathChoice' => './/qti:inlineChoice'], 'Graphic hotspot' => ['domInteraction' => 'hotspotInteraction', 'xpathChoice' => './/qti:hotspotChoice'], 'Graphic order' => ['domInteraction' => 'graphicOrderInteraction', 'xpathChoice' => './/qti:hotspotChoice'], 'Graphic associate' => ['domInteraction' => 'graphicAssociateInteraction', 'xpathChoice' => './/qti:associableHotspot'], 'Graphic gap match' => ['domInteraction' => 'graphicGapMatchInteraction', 'xpathChoice' => './/qti:gapImg'], 'ScaffHolding' => ['xpathInteraction' => '//*[@customInteractionTypeIdentifier="adaptiveChoiceInteraction"]', 'xpathChoice' => 'descendant::*[@class="qti-choice"]'], 'Extended text' => ['domInteraction' => 'extendedTextInteraction'], 'Slider' => ['domInteraction' => 'sliderInteraction'], 'Upload file' => ['domInteraction' => 'uploadInteraction'], 'Text entry' => ['domInteraction' => 'textEntryInteraction'], 'End attempt' => ['domInteraction' => 'endAttemptInteraction']];
/**
* foreach all interactions type
*/
foreach ($elements as $element => $parser) {
if (isset($parser['domInteraction'])) {
$interactionNode = $this->dom->getElementsByTagName($parser['domInteraction']);
} elseif (isset($parser['xpathInteraction'])) {
$interactionNode = $this->xpath->query($parser['xpathInteraction']);
} else {
continue;
}
if ($interactionNode->length == 0) {
continue;
}
/**
* foreach all real interactions
*/
for ($i = 0; $i < $interactionNode->length; $i++) {
$interaction = [];
$interaction['id'] = uniqid();
$interaction['type'] = $element;
$interaction['choices'] = [];
$interaction['responses'] = [];
/**
* Interaction right answers
*/
$interaction['responseIdentifier'] = $interactionNode->item($i)->getAttribute('responseIdentifier');
$rightAnswer = $this->xpath->query('./qti:responseDeclaration[@identifier="' . $interaction['responseIdentifier'] . '"]');
if ($rightAnswer->length > 0) {
$answers = $rightAnswer->item(0)->textContent;
if (!empty($answers)) {
foreach (explode(PHP_EOL, $answers) as $answer) {
if (trim($answer) !== '') {
$interaction['responses'][] = $answer;
}
}
}
}
/**
* Interaction choices
*/
$choiceNode = '';
if (!empty($parser['domChoice'])) {
$choiceNode = $this->dom->getElementsByTagName($parser['domChoice']);
} elseif (!empty($parser['xpathChoice'])) {
$choiceNode = $this->xpath->query($parser['xpathChoice'], $interactionNode->item($i));
}
if (!empty($choiceNode) && $choiceNode->length > 0) {
for ($j = 0; $j < $choiceNode->length; $j++) {
$identifier = $choiceNode->item($j)->getAttribute('identifier');
$value = $this->sanitizeNodeToValue($this->dom->saveHtml($choiceNode->item($j)));
//Image
if ($value === '') {
$imgNode = $this->xpath->query('./qti:img/@src', $choiceNode->item($j));
if ($imgNode->length > 0) {
$value = 'image' . $j . '_' . $imgNode->item(0)->value;
}
}
$interaction['choices'][$identifier] = $value;
}
}
$this->interactions[] = $interaction;
}
}
return $this;
}