26 April 2012

Detect character encoding in PHP and convert string to UTF-8.

Source code viewer
  1. $string = 'encoded text';
  2. $encodings = array('UTF-8', 'ISO-8859-1', 'ISO-8859-15');
  3.  
  4. $encoding = mb_detect_encoding($string, $encodings);
  5.  
  6. $utf8_string = iconv($encoding, "UTF-8//TRANSLIT", $string);
Programming Language: PHP