PHP problem character set
<p>I have a problem where users upload zipped text files. After  I extract text contents I import them in mysql database. But later when I display the text in browser some characters are garbled. I tried to encode them but I am unable to detect the encoding of the text files with PHP and convert to UTF-8 with iconv or mbstring.</p>

<p>Mysql database charset is UTF-8.</p>

<pre><code>header('Content-type: text/html; charset=utf-8');
</code></pre>

<p>is added.</p>

<p>Tried with 
    iconv('UTF-8', 'UTF-8//IGNORE', $text_file_contents)</p>

<p>But it simply removes the garbled chars: � which should be either ' or " when I checked manually with Firefox browser. Firefox showed that is ISO-8859-1 but I can not check for every article they send (articles may be in different character set).</p>

<p>How to convert this characters to UTF-8 ?</p>

<p>EDIT:
 This is a modified function I found on 
<a href="http://php.net/manual/en/function.mb-detect-encoding.php" rel="nofollow">http://php.net/manual/en/function.mb-detect-encoding.php</a>
origanlly written by prgss at bk dot ru .</p>

<pre>


function myutf8_detect_encoding($string, $default = 'UTF-8', $encode = 0, $encode_to = 'UTF-8') { 
  static $list = array('UTF-8', 'ISO-8859-1', 'ASCII', 'windows-1250', 'windows-1251', 'latin1', 'windows-1252', 'windows-1253', 'windows-1254', 'windows-1255', 'windows-1256', 'windows-1257', 'windows-1258', 'ISO-8859-2', 'ISO-8859-3', 'GBK', 'GB2312', 'GB18030', 'MACROMAN', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-11', 'ISO-8859-12', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16');

  foreach ($list as $item) {
    $sample = iconv($item, $item, $string);
    if (md5($sample) == md5($string)) {

      if ($encode == 1)
      return iconv($item, $encode_to, $string);
      else
      return $item;
    }
  }

  if ($encode == 1)
  return iconv($encode_to, $encode_to . '//IGNORE', $string);
  else
  return $default;
}
</pre> 

<p>and in my code I use:</p>

<pre><code>myutf8_detect_encoding(trim($description), 'UTF-8', 1)
</code></pre>

<p>but it still returns garbled characters of this text  “old is gold’’ .</p>
