PHP htmlentities并以xml格式保存数据 [英] PHP htmlentities and saving the data in xml format

查看:87
本文介绍了PHP htmlentities并以xml格式保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下PHP脚本将一些数据保存到xml文件中:

Im trying to save some data into a xml file using the following PHP script:

<?php

$string = '<a href="google.com/maps">Go to google maps</a> and some special characters ë è & ä etc.';

$string = htmlentities($string, ENT_QUOTES, 'UTF-8');

$doc = new DOMDocument('1.0', 'UTF-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;

$root = $doc->createElement('top');
$root = $doc->appendChild($root);

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$id = $doc->createAttribute('id');
$id->value = '1';
$text = $title->appendChild($id);

$text = $doc->createTextNode($string);
$text = $title->appendChild($text);

$doc->save('data.xml');

echo 'data saved!';

?>

我正在使用htmlentities将所有字符串转换为html格式,如果我不这样做,则不会将特殊字符转换为html格式.这是输出:

I'm using htmlentities to translate all of the string into an html format, if I leave this out the special characters won't be translated to html format. this is the output:

<?xml version="1.0" encoding="UTF-8"?>
<top>
  <title id="1">&amp;lt;a href=&amp;quot;google.com/maps&amp;quot;&amp;gt;Go to google maps&amp;lt;/a&amp;gt; and some special characters &amp;euml; &amp;egrave; &amp;amp; &amp;auml; etc.</title>
</top>

html标记的&"号得到一个双HTML代码:&amp;lt;,而&"号变为:&amp;amp;

The ampersand of the html tags get a double html code: &amp;lt; and an ampersand becomes: &amp;amp;

这是正常行为吗?或如何防止这种情况发生?看起来像是双重编码.

Is this normal behavior? Or how can I prevent this from happening? Looks like a double encoding.

推荐答案

尝试删除该行:

$string = htmlentities($string, ENT_QUOTES, 'UTF-8');

因为传递给createTextNode()的文本还是被转义了.

Because the text passed to createTextNode() is escaped anyway.

更新: 如果您要转义utf-8字符.您可以离开该行,然后尝试直接在createElement()中添加$ string.

Update: If you want the utf-8 characters to be escaped. You could leave that line and try to add the $string directly in createElement().

例如:

$title = $doc->createElement('title', $string);
$title = $root->appendChild($title);

在PHP 文档中,它说$ string不会被转义.我还没有尝试过,但是应该可以.

In PHP documentation it says that $string will not be escaped. I haven't tried it, but it should work.

这篇关于PHP htmlentities并以xml格式保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆