submitted by: Snippissimo

PHP: Convert string to UTF-8 for MySQL

This very useful function will convert a string to utf-8, allowing you update a MySQL table that has collation conflicts. For example if you've imported a latin1 charset and need to display it in utf-8


code snippet:
<?php
// Fixes the encoding to utf8
function fixEncoding($in_str)
{
  
cur_encoding mb_detect_encoding($in_str) ;
  if(
$cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
    return 
$in_str;
  else
    return 
utf8_encode($in_str);
}
?>

notes:
If you're using the output of this function to update your MySQL database, don't forget to addslashes()