進出口數據嘗試使用下面的PHP腳本來保存一些數據到一個XML文件:PHPヶ輛和保存在XML格式
<?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!';
?>
我使用ヶ輛所有的字符串轉換成HTML格式,如果我離開這個特殊字符將不會被翻譯成html格式。這是輸出:
<?xml version="1.0" encoding="UTF-8"?>
<top>
<title id="1">&lt;a href=&quot;google.com/maps&quot;&gt;Go to google maps&lt;/a&gt; and some special characters &euml; &egrave; &amp; &auml; etc.</title>
</top>
html標籤得到雙重的HTML代碼的符號:&lt;
和符號變爲:&amp;
這是正常的行爲呢?或者我怎樣才能防止這種情況發生?看起來像一個雙重編碼。
當我刪除該行時,特殊字符不會轉換爲HTML代碼。就像'必須變成ë如果我離開那條線,你知道如何做到這一點嗎? – Daniel
我已經更新了我的回答 –
Thx您的回覆!如果我直接添加它,你的權利它不會逃避字符串。但是現在我有一個XML解析錯誤:未定義的實體,因爲它不能將ë保存在字符串中。 – Daniel