如何使用PHP文档打印未缩放的HTML
我在JQuery&PHP中有一个简单的本地运行的应用程序,它允许用户打开一个文件,重新排列它的DOM元素,然后用重新排列的元素生成一个新文件。问题是PHP文档正在打印转义的HTML。我需要打印newfile的内容,以$content的形式由POST接收,并将其打印为unscaped HTML POST函数如下所示:如何使用PHP文档打印未缩放的HTML,php,html,Php,Html,我在JQuery&PHP中有一个简单的本地运行的应用程序,它允许用户打开一个文件,重新排列它的DOM元素,然后用重新排列的元素生成一个新文件。问题是PHP文档正在打印转义的HTML。我需要打印newfile的内容,以$content的形式由POST接收,并将其打印为unscaped HTML POST函数如下所示: $("#submit").click(function() { var str = $('#newfile').html(); $.post("genera
$("#submit").click(function() {
var str = $('#newfile').html();
$.post("generate.php", { content: str} );
});
<?php
$content = $_POST['content'];
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('html');
$root = $doc->appendChild($root);
$head = $doc->createElement('head');
$head = $root->appendChild($head);
$body = $doc->createElement('body');
$body = $root->appendChild($body);
$element = $doc->createElement('div', $content);
$element = $body->appendChild($element);
echo 'Wrote: ' . $doc->saveHTMLFile("output/test.html") . ' bytes';
?>
generate.php文件如下所示:
$("#submit").click(function() {
var str = $('#newfile').html();
$.post("generate.php", { content: str} );
});
<?php
$content = $_POST['content'];
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('html');
$root = $doc->appendChild($root);
$head = $doc->createElement('head');
$head = $root->appendChild($head);
$body = $doc->createElement('body');
$body = $root->appendChild($body);
$element = $doc->createElement('div', $content);
$element = $body->appendChild($element);
echo 'Wrote: ' . $doc->saveHTMLFile("output/test.html") . ' bytes';
?>
输出当前是格式良好的HTML文档,但我需要的内容已转义:
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
<body><div>
<div class="page">
<h3>1. Definition</h3>
<div id="definition" class="output ui-droppable">
</div>
</div>
</div></body></html>
如何将$content打印为未转换的HTML?HTML\u entity\u decode与htmlentities相反,它将字符串中的所有HTML实体转换为它们适用的字符
<?php
$orig = "I'll \"walk\" the <b>dog</b> now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll "walk" the <b>dog</b> now
echo $b; // I'll "walk" the <b>dog</b> now
?>
Ref:我使用了方法-createDocumentFragment+appendXML。我的final generate.php如下所示,工作非常完美:
<?php
$content = $_POST['content'];
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('html');
$root = $doc->appendChild($root);
$head = $doc->createElement('head');
$head = $root->appendChild($head);
$body = $doc->createElement('body');
$body = $root->appendChild($body);
$f = $doc->createDocumentFragment();
$f->appendXML($content);
$body->appendChild($f);
echo 'Wrote: ' . $doc->saveHTMLFile("output/test.html") . ' bytes'; // Wrote: 129 bytes
?>
您能建议如何将其合并到DOMDocument中,以便打印$content的值时不带任何痕迹吗?我已经用上面的构造尝试过了,但没有什么区别——它仍然可以打印。