title: PHP htmlentities and saving the data in xml format
body: <p>Im trying to save some data into a xml file using the following PHP script:</p>
  
  <pre><code>&lt;?php
  
  $string = '&lt;a href="google.com/maps"&gt;Go to google maps&lt;/a&gt; and some special characters ë è &amp; ä etc.';
  
  $string = htmlentities($string, ENT_QUOTES, 'UTF-8');
  
  $doc = new DOMDocument('1.0', 'UTF-8');
  $doc-&gt;preserveWhiteSpace = false;
  $doc-&gt;formatOutput = true;
  
  $root = $doc-&gt;createElement('top');
  $root = $doc-&gt;appendChild($root);
  
  $title = $doc-&gt;createElement('title');
  $title = $root-&gt;appendChild($title);
  
  $id = $doc-&gt;createAttribute('id');
  $id-&gt;value = '1';
  $text = $title-&gt;appendChild($id);
  
  $text = $doc-&gt;createTextNode($string);
  $text = $title-&gt;appendChild($text);
  
  $doc-&gt;save('data.xml');
  
  echo 'data saved!';
  
  ?&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;top&gt;
    &lt;title id="1"&gt;&amp;amp;lt;a href=&amp;amp;quot;google.com/maps&amp;amp;quot;&amp;amp;gt;Go to google maps&amp;amp;lt;/a&amp;amp;gt; and some special characters &amp;amp;euml; &amp;amp;egrave; &amp;amp;amp; &amp;amp;auml; etc.&lt;/title&gt;
  &lt;/top&gt;
  </code></pre>
  
  <p>The ampersand of the html tags get a double html code: <code>&amp;amp;lt;</code> and an ampersand becomes: <code>&amp;amp;amp;</code></p>
  
  <p>Is this normal behavior? Or how can I prevent this from happening? Looks like a double encoding.</p>