title: Convert a UTF-8 string to/from 7-bit XML in PHP
body: <p>How can UTF-8 strings (i.e. 8-bit string) be converted to/from XML-compatible 7-bit strings (i.e. printable ASCII with numeric entities)?</p>
  
  <p>i.e. an <code>encode()</code> function such that:</p>
  
  <pre><code>encode("“£”") -&gt; "&amp;#8220;&amp;#163;&amp;#8221;"
  </code></pre>
  
  <p><code>decode()</code> would also be useful:</p>
  
  <pre><code>decode("&amp;#8220;&amp;#163;&amp;#8221;") -&gt; "“£”"
  </code></pre>
  
  <p>PHP's <code>htmlenties()</code>/<code>html_entity_decode()</code> pair does not do the right thing:</p>
  
  <pre><code>htmlentities(html_entity_decode("&amp;#8220;&amp;#163;&amp;#8221;")) -&gt;
    "&amp;amp;#8220;&amp;pound;&amp;amp;#8221;"
  </code></pre>
  
  <p>Laboriously specifying types helps a little, but still returns XML-incompatible named entities, not numeric ones:</p>
  
  <pre><code>htmlentities(html_entity_decode("&amp;#8220;&amp;#163;&amp;#8221;", ENT_QUOTES, "UTF-8"), ENT_QUOTES, "UTF-8") -&gt;
    "&amp;ldquo;&amp;pound;&amp;rdquo;"
  </code></pre>