title: PHP and dealing with foreign characters for UTF-8 XML
body: <p>I am currently scraping some data from the internet and converting into xml documents.</p>
  
  <ul>
  <li>document being scraped is utf-8 according to its meta tags</li>
  </ul>
  
  <p>The problem is some of the data contains foreign characters, I cannot find a way of reliably converting them into XML / utf-8 friendly entities, the following errors are what I have managed to find by reading through, I would ideally like a solution that would work all the time.</p>
  
  <p>Example 1 works correctly, example 2 fails. My research fixed example 1, but it does not seem to be a blanket solution.</p>
  
  <pre>
  Côte d'Ivoire  
  C&amp;#244;te d'Ivoire (correct)  
  </pre>
  
  <p>I managed to get the - ô - parsing correctly using the following function on my xpath.</p>
  
  <pre><code>$w-&gt;text(charset_decode_utf_8((string)$match-&gt;a));
  
  function charset_decode_utf_8($string) {
      if(@!ereg("[\200-\237]",$string) &amp;&amp; @!ereg("[\241-\377]",$string)) {
          return $string;
      }
  $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e","'&amp;#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",$string);
  $string = preg_replace("/([\300-\337])([\200-\277])/e","'&amp;#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",$string);
  return $string;
  }
  </code></pre>
  
  <pre>
  ÖFB Stiegl Cup  
  &#195;&#8211;FB Stiegl Cup (wrong)  
  </pre>
  
  <p>Unfortunately on the - Ö - it gets converted into a double entity. I have no idea how to make it convert to a proper html entity.</p>
  
  <p>I have tried:</p>
  
  <ul>
  <li>using iso-8859-1 encoding when creating my xml document</li>
  <li>using htmlentities with utf-8 encoding</li>
  </ul>
  
  <p>Any help would be greatly appreciated, as I am tearing my hair out trying to get things to save correctly.</p>