EUC-KR<=>UTF-8 »óÈ£º¯È¯ Çϱâ
<?
# UTF ·Î ¹«Á¶°Ç º¯È¯
function change_to_utf($utfStr) {
if (iconv("UTF-8","UTF-8",$utfStr) == $utfStr) {
return $utfStr;
}
else {
return iconv("EUC-KR","UTF-8",$utfStr);
}
}
# UTF => EUC-KR·Î º¯È¯
function iconv_to_euc_kr($get_data) {
$work_unit = 50;
$init_size = strlen($get_data);
$result_data = "";
$count = 0;
for ( $i = 0 ; $i < $init_size ; $i++ ) {
$cur_char = substr($get_data,$i,1);
$t = ord($cur_char);
if ( $t == 9 || $t == 10 || (32 <= $t && $t <= 126) ) {
$tn = 1;
}
else if ( 194 <= $t && $t <= 223 ) {
$tn = 2;
}
else if ( 224 <= $t && $t < 239 ) {
$tn = 3;
}
else if ( 240 <= $t && $t <= 247 ) {
$tn = 4;
}
else if ( 248 <= $t && $t <= 251 ) {
$tn = 5;
}
else if ( $t == 252 || $t == 253 ) {
$tn = 6;
}
else {
$tn = 1;
}
if ( $work_unit < $tn ) {
break;
}
if ( $count + $tn > $work_unit ) {
$temp_data = iconv("utf-8","euc-kr",$work_string);
$result_data .= $temp_data;
$work_string = "";
$i--;
$count = 0;
}
else {
for ( $j = 0 ; $j < $tn ; $j++ ) {
$work_string .= $cur_char;
$i++;
$count++;
$cur_char = substr($get_data,$i,1);
}
$i--;
}
}
if ( $work_string ) {
$temp_data = iconv("utf-8","euc-kr",$work_string);
$result_data .= $temp_data;
}
return $result_data;
}
# ex.
# ¼ö½ÅµÈ ¸ÞÀϷκÎÅÍ Æû°ªÀ» ¹ÞÀ»¶§ ¼ö½ÅµÈ ¸ÞÀÏÀÇ charsetÀÌ euc-krÀÌ ¾Æ´Ñ UTF8·Î ¿À´Â °æ¿ì,
# ÀÏ´Ü ¹«Á¶°Ç UTF8·Î º¯È¯ÇÑ ÈÄ EUC-KR·Î À纯ȯÇÏ¿© ó¸®Çؾ߸¸ ÇѱÛÀÌ ±úÁöÁö ¾Ê°í
# Á¤»óÀûÀ¸·Î Ç¥ÃâµË´Ï´Ù.
# Usage.
# $fromName = change_to_utf($fromName);
# $fromName = iconv_to_euc_kr($fromName);
?>
/**
* UTF-8=>ÇÑ±Û ÄÚµåÆäÀÌÁö[949]·Î º¯È¯ AJAX¿¡¼ À¯´ÏÄÚµå ¹®ÀÚ ¿À·ù
*/
function utf8_euckr(&$item, $key, $prefix = ''){
if(is_array($item)) array_walk($item, 'utf8_euckr');
else $item=iconv('UTF-8', 'CP949',$item);
}
/**
* ÇÑ±Û ÄÚµåÆäÀÌÁö[949] => UTF-8·Î º¯È¯ AJAX¿¡¼ À¯´ÏÄÚµå ¹®ÀÚ ¿À·ù
*/
function euckr_utf8(&$item, $key, $prefix = ''){
if(is_array($item)) array_walk($item, 'euckr_utf8');
else $item=iconv('CP949', 'UTF-8',$item);
}
/**
* ÀԷ°ª
*/
@array_walk($_POST, 'utf8_euckr');
/**
* °á°ú°ª º¯È¯
*/
@array_walk($Result, 'euckr_utf8');