所以我试图创建一种方法来为我的一个项目生成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><p>djqshdkjqhsd</p><p><em>qzdqzdqz</em></p><p><em><strong>qdqdqz</strong></em></p></body>
</html>