title: PHP function to turn arbitrary "description" into valid xml data for podcast feed
body: <p>I am reading the documentation for creating a podcast feed <a href="http://www.apple.com/itunes/podcasts/specs.html" rel="nofollow noreferrer">suitable for iTunes</a>, and the <a href="http://www.apple.com/itunes/podcasts/specs.html#commonmistakes" rel="nofollow noreferrer"><strong>Common Mistakes</strong></a> section says:</p>
  
  <hr>
  
  <p><strong>Using HTML Named Character Entities.</strong></p>
  
  <pre><code>&lt;! — illegal xml — &gt;
  &lt;copyright&gt;&amp;copy; 2005 John Doe&lt;/copyright&gt;
  
  &lt;! — valid xml — &gt;
  &lt;copyright&gt;&amp;#xA9; 2005 John Doe&lt;/copyright&gt;
  </code></pre>
  
  <blockquote>
    <p>Unlike <a href="http://www.w3.org/TR/REC-html40/sgml/entities.html#h-24.2.1" rel="nofollow noreferrer">HTML</a>, XML supports only five
    "named character entities":</p>
  </blockquote>
  
  <pre><code>character   name               xml
  &amp;           ampersand          &amp;amp;
  &lt;           less-than sign     &amp;lt;
  &gt;           greater-than sign  &amp;gt;
  ’           apostrophe         &amp;apos;
  "           quotation          &amp;quot;
  </code></pre>
  
  <blockquote>
    <p>The five characters above are the only
    characters that require escaping in
    XML. All other characters can be
    entered directly in an editor that
    supports UTF-8. You can also use
    numeric character references that
    specify the Unicode for the character,
    for example:</p>
  </blockquote>
  
  <pre><code>character   name                       xml
  ©           copyright sign             &amp;#xA9;
  ℗           sound recording copyright  &amp;#x2117;
  ™           trade mark sign            &amp;#x2122;
  </code></pre>
  
  <blockquote>
    <p>For further reference see <a href="http://www.xml.com/axml/target.html#sec-references" rel="nofollow noreferrer">XML
    Character and EntityReferences</a>.</p>
  </blockquote>
  
  <hr>
  
  <p>Right now I'm using <code>htmlentities()</code> under PHP5 and the feed is validating and working. But from what I gather some things that could get put into content might become entities that would make it no longer be valid. What's the best function to use to assure I'm not passing along bad data? I'm paranoid something will get entered and get entity-ized and break the feed -- should I just use <code>str_replace()</code> and replace with named entities and leave the rest alone? Or can I use <code>htmlspecialchars()</code> somehow?</p>
  
  <p>So in short, what's a drop-in replacement for <code>htmentities()</code> that will make sure input is safe for description, titles, etc in a podcast RSS feed?</p>