Wednesday, November 28, 2012

Detecting character encoding in PHP

Dealing with character encoding is never fun. It's easy to know that something has gone wrong when text is displaying as hieroglyphics, but it can be difficult to figure out why.

I found myself in this position trying to fix some Japanese text the other day and wanted to write a short example of  mb_detect_encoding(); before I forget it.

<?php
// a list of possible encodings
$encodings = array('UTF-8','windows-1251','ISO-8859-1','ASCII','JIS','EUC-JP');

$string = "Example text";  // the text you wish to test
$in_encoding = mb_detect_encoding($string, $encodings, true);
echo "Encoding type: ".$in_encoding;
?>

 If you find that your encoding is something other than what you desire, you should check out iconv();

No comments:

Post a Comment