【发布时间】:2015-04-23 05:59:04
【问题描述】:
给定一些 HTML,我将 http://php.net/manual/en/class.domdocument.php 类应用到它,保存它,并且偶尔会插入  符号。它似乎发生在具有单个空格(与 &nbsp; 相对)的标签上,但似乎不是绝对的(只有第一个 <span> 元素表现出这种现象)。我尝试按照PHP DOMDocument->getElementByID adding  in place of empty <span> 的建议在显示生成的 HTML 时添加编码,但是问题仍然存在。是什么原因造成的,如何预防?
如果您对我这样做的原因感兴趣。我有一个应用程序,我用文本替换 HTML 图像。在将 HTML 从 Outlook 电子邮件复制并粘贴到 TinyMCE 编辑器,然后解析 HTML 时,我遇到了这种行为。
<?php
$message = <<<EOT
<p>Start</p>
<p> </p>
<p> </p>
<p></p>
<p class="MsoNormal">
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br /></span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br /></span>
<span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;">Phone: (444) 777-7777</span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br /></span>
</p>
<p>End</p>
EOT;
echo('<p>Initial HTML:</p> '.$message);
$message_encoded = utf8_encode($message);
$doc = new DOMDocument();
$doc->loadHTML($message);
$body = $doc->getElementsByTagName('body')->item(0);
$message=$doc->saveHTML($body);
echo('<p>Final HTML:</p> '.$message);
echo('<p>Initial HTML encoded:</p> '.$message_encoded);
$doc->loadHTML($message_encoded);
$body = $doc->getElementsByTagName('body')->item(0);
$message_encoded=$doc->saveHTML($body);
echo('<p>Final HTML:</p> '.$message_encoded);
?>
输出:
<p>Initial HTML:</p> <p>Start</p>
<p> </p>
<p> </p>
<p></p>
<p class="MsoNormal">
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br /></span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br /></span>
<span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;">Phone: (444) 777-7777</span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br /></span>
</p>
<p>End</p><p>Final HTML:</p> <body>
<p>Start</p>
<p>Â </p>
<p>Â </p>
<p></p>
<p class="MsoNormal">
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;">Â <br></span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br></span>
<span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;">Phone: (444)Â 777-7777</span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br></span>
</p>
<p>End</p>
</body><p>Initial HTML encoded:</p> <p>Start</p>
<p>Â </p>
<p>Â </p>
<p></p>
<p class="MsoNormal">
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;">Â <br /></span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br /></span>
<span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;">Phone: (444)Â 777-7777</span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br /></span>
</p>
<p>End</p><p>Final HTML:</p> <body>
<p>Start</p>
<p>ÃÂ </p>
<p>ÃÂ </p>
<p></p>
<p class="MsoNormal">
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;">ÃÂ <br></span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br></span>
<span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;">Phone: (444)ÃÂ 777-7777</span>
<span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"> <br></span>
</p>
<p>End</p>
</body>
【问题讨论】:
标签: php html dom domdocument