title: how to handle all special characters in HTML/php form that outputs to XML
body: <p>I have a little php/mysql app I put together that takes an input form and stores it in a MySQL database, and outputs the data as XML for consumption by a radio-playing hardware device. </p>
  
  <p>The problem is ampersands and other characters. The user is taking descriptions of various radio stations, along with streaming URL or Playlist URL and pasting them into the form. Some radio stations are in non-english speaking countries (mostly French). I need to know what to do to preprocess these fields so that the XML that is generated is not corrupted, which breaks the external hardware app.</p>
  
  <p>I assume that this should go into the php that is called when the form is submitted. I'm pretty sure the <strong>htmlspecialchars</strong> function should be used, but I'm not sure the best method, since I've hacked this together from a variety of sources:</p>
  
  <p>UPDATE: Here is my current output code with some regex that cleans up the ampersands. </p>
  
  <pre><code>&lt;?
  include("HLN/manager/connect.php");
  
  $query = "SELECT * FROM hln_stations ORDER BY orderid ASC";
  $result = mysql_query($query);
  
  $num = mysql_num_rows ($result);
  mysql_close();
  
  $xml = new XMLWriter();
  
  $xml-&gt;openURI("php://output");
  $xml-&gt;startDocument();
  header('Content-type: text/xml');
  $xml-&gt;setIndent(true);
  
  $xml-&gt;startElement('channels');
  
  while ($row = mysql_fetch_assoc($result)) {
  
    $xml-&gt;startElement("channel");
       $xml-&gt;startElement("title");
            $xml-&gt;writeRaw(preg_replace('/&amp;(?![A-Za-z0-9#]{1,7};)/','&amp;amp;',$row['station_title']));
       $xml-&gt;endElement();
       $xml-&gt;startElement("descriptionline1");
            $xml-&gt;writeRaw(preg_replace('/&amp;(?![A-Za-z0-9#]{1,7};)/','&amp;amp;',$row['station_display_name']));
       $xml-&gt;endElement();
  
       $xml-&gt;startElement("descriptionline2");
            $xml-&gt;writeRaw(preg_replace('/&amp;(?![A-Za-z0-9#]{1,7};)/','&amp;amp;',$row['station_subtitle']));
       $xml-&gt;endElement();
  
       $xml-&gt;startElement("description");
            $xml-&gt;writeRaw(preg_replace('/&amp;(?![A-Za-z0-9#]{1,7};)/','&amp;amp;',$row['station_detailed_description']));
       $xml-&gt;endElement();
  
       $xml-&gt;startElement("sdimage");
            $xml-&gt;writeRaw(preg_replace('/&amp;(?![A-Za-z0-9#]{1,7};)/','&amp;amp;',$row['sdtv_thumbnail_graphic_url']));
       $xml-&gt;endElement();
  
       $xml-&gt;startElement("hdimage");
            $xml-&gt;writeRaw(preg_replace('/&amp;(?![A-Za-z0-9#]{1,7};)/','&amp;amp;',$row['hdtv_thumbnail_graphic_url']));
       $xml-&gt;endElement();
  
       $xml-&gt;startElement("uri");
            $xml-&gt;writeRaw(preg_replace('/&amp;(?![A-Za-z0-9#]{1,7};)/','&amp;amp;',$row['stream_url_or_playlist_url']));
       $xml-&gt;endElement();
  
       $xml-&gt;startElement("linktype");
            $xml-&gt;writeRaw(preg_replace('/&amp;(?![A-Za-z0-9#]{1,7};)/','&amp;amp;',$row['link_type']));
       $xml-&gt;endElement();
  
   $xml-&gt;endElement();
  }
  
  $xml-&gt;endElement();
  
  
  $xml-&gt;flush();
  
  ?&gt;
  </code></pre>
  
  <p>But I still need to solve the French character set issues that are cropping up. How can I replace the é character for example with something that doesn't cause problems?</p>