March 9, 2012

Textarea not showing hebrew text from mysql

Question by Prateek

I’m trying to return some particular text from mysql into a textarea. Now this text returning from mysql has

    collation=utf8_unicode_ci 

and the charset is set by adding this line in my php file.

   <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-8-i" />

Cant figure out the problem here. Everything looks alright but why is the textarea not showing hebrew text? Instead it shows “???????” – Question marks.

Thanks.

Answer by Starx

During the data entry, or retrieval, the MySQL must be configured to accept unicode(UTF-8) data. There are number of ways, you can do this

  1. Place this line on the database connection part

    mysql_set_charset('utf8',$link); //$link is your connection identifier
    
  2. Run charset utf8 on CLI

  3. Use SET NAMES 'utf8' as your first query to the database

Moreover, while outputting such data the screen, the html should be configured to understand unicode characters as well. Use

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

At the top of the displaying page.

March 2, 2012

How to get OS default encoding?

Question by alex347

What is the proper way of getting default OS encoding? For Linux it can be found here: /etc/sysconfig/i18n

If you think the best way is to read from that file, then can I rely it will work on all modern major Linux distributions? What about Windows?

Answer by Starx

The best way to detect encoding, is from the piece of text you are trying to read from.

Use mb_detect_encoding()[docs here] function

$str = "....."; //use you own logic to get the text
echo mb_detect_encoding($str);

Adding on to @Evert

Encoding happens when characters are displayed on the screen or CLI interface. It is not OS dependent, rather content specific.

...

Please fill the form - I will response as fast as I can!