有没有办法强迫php DOMDOcument在创建HTML文件时不替换我的“ <”和“>”?

时间:2019-05-10 08:08:52

标签: php html domdocument

所以我试图创建一种方法来为我的一个项目生成html电子邮件模板,我的想法是使用DOMDocument php创建文件,并使用WYSIWYG编辑器创建文件内容。一切正常,除了当我通过编辑器编写的内容并且DOM创建html很好时,它将“ <”和““>”替换为“&lt”和“&gt”之类的东西。我知道禁止使用像<,>和&之类的DOMDocument,但是有办法避免这种情况吗?

我当时想打开文件,寻找“&lt”和“&gt”然后替换它们,但我认为这不是最好的解决方案。

以下代码负责创建html模板。请注意,title和template变量来自另一个页面,您实际上可以在其中键入标题和模板的内容。

<?php 
$title = "mail_template/".$_POST['title'].".html";
$contenue = $_POST['template'];
var_dump($title);
var_dump($contenue);

$template = new DOMDocument;
$template->formatOutput = true;

$root = $template->createElement('html');
$template->appendChild($root);


$head = $template->createElement('head');
$root->appendChild($head);

$meta = $template->createElement('meta');
$metaAttribute = $template->createAttribute('charset');
$metaAttribute->value = "utf-8";
$meta->appendChild($metaAttribute);
$head->appendChild($meta);

$body = $template->createElement('body');
$root->appendChild($body);

$content = $template->createTextNode($contenue);
$body->appendChild($content);

$template->saveHTMLFile($title);
?>

以及输出文件的外观:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
</head>
<body>&lt;p&gt;djqshdkjqhsd&lt;/p&gt;&lt;p&gt;&lt;em&gt;qzdqzdqz&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;qdqdqz&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;</body>
</html>

0 个答案:

没有答案