tags:

views:

24

answers:

1

Hi, i have this string in my utf-8 mysql DB: "Pruebá de eñes"

When i print it like plain text, everything works ok, but if i load that same field inside an input, textarea, etc, it becomes: "Pruebá de eñes"

How can i solve this problem? =(

+1  A: 

First I recommend read this: http://www.joelonsoftware.com/articles/Unicode.html (The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky) really useful.

Then if it's MySQL you can set the default character set in the connection by "SET NAMES 'utf8'". For example in Zend_Framework ($db->query("SET NAMES 'utf8'");)

Another option is add a filter to the data before print it in the screen. I made two handly functions ...

function utf8encode ($subject, $action = '//TRANSLIT')
{
   if ( @iconv( 'UTF-8', "UTF-8{$action}", $subject) == $subject ) {
                    #-- the value it's utf8
  } else {
                    $subject = utf8_encode ($subject);
  }

 return $subject;

}

function utf8decode ($subject, $action = '//TRANSLIT')
{
    if ( @iconv( 'UTF-8',  "UTF-8{$action}", $subject) == $subject ){
                      $subject = utf8_decode ($subject);
    } else {
                      #-- the value is probably ISO-8859-1
    }

 return $subject;
}
Mahomedalid