Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2013 15:58
Show Gist options
  • Save anonymous/4674232 to your computer and use it in GitHub Desktop.
Save anonymous/4674232 to your computer and use it in GitHub Desktop.
Convert tis-620 to utf-8 text function (for legacy data from database).
<?php
function tis620_to_utf8($text) {
$utf8 = "";
for ($i = 0; $i < strlen($text); $i++) {
$a = substr($text, $i, 1);
$val = ord($a);
if ($val < 0x80) {
$utf8 .= $a;
} elseif ((0xA1 <= $val && $val < 0xDA) || (0xDF <= $val && $val <= 0xFB)) {
$unicode = 0x0E00+$val-0xA0; $utf8 .= chr(0xE0 | ($unicode >> 12));
$utf8 .= chr(0x80 | (($unicode >> 6) & 0x3F));
$utf8 .= chr(0x80 | ($unicode & 0x3F));
}
}
return $utf8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment