Charset Auto Detection(캐릭터셋 자동 감지)

명시적으로 캐릭터셋을 정하지 않는 상태에서
param 으로 넘어온값이나, 문자열의 charset을 검출해야 하는 경우가 있습니다.
흔하게 발생되는 경우는 아니지만, 외부 시스템 연동과 같은 특수상황에서는 종종 발생하는 케이스입니다.
자바쪽 자료를 찾다가 php 자료들도 보이길래 정리해봅니다.
자동검출이기 때문에 100% 완벽하지 않습니다.

1. mb_detect_encoding()
- http://kr.php.net/manual/en/function.mb-detect-encoding.php

2. 캐릭터셋의 바이트 구조를 분석하여 직접 코딩
- http://my.oops.org/62
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: JoungKyun Kim <http://www.oops.org> |
// +----------------------------------------------------------------------+
//
// $Id: eSystem.php,v 1.2 2005/07/11 05:58:11 oops Exp $

require_once 'PEAR.php';

/**
* PEAR's eSystem:: interface. Defines the php extended system mapping function
* and any utility mapping function
*
* @access public
* @version $Revision: 0.1 $
* @package Unicode
*/
class Unicode extends PEAR
{
function chr2bin ($c, $shift = '') {
$c = ord ($c);

if ( $shift && preg_match ('/^([<>]+)[\s]*([0-9]+)/', $shift, $match) ) :
switch ($match[1]) :
case '>>' : $c = $c >> $match[2]; break;
case '<<' : $c = $c << $match[2]; break;
case '<' : $c = $c < $match[2]; break;
case '>' : $c = $c > $match[2]; break;
endswitch;
endif;

return decbin ($c);
}

function is_utf8 ($str) {
$_l = strlen ($str);
$_not = 0;

for ( $i=0; $i<$_l; $i++ ) :
#$_first = $this->chr2bin ($str[$i]);

# if 7bit charactior or numeric, skipped
#if ( strlen ($_first) != 8 )
# continue;

# if single byte charactors, skipped
if ( ! (ord ($str[$i]) & 0x80) ) :
continue;
endif;

$_first = $this->chr2bin ($str[$i], '>>4');

switch ( $_first ) :
case 1111 : $b = 3; break; # 4byte
case 1110 : $b = 2; break; # 3byte
default : return 0; # not utf8
endswitch;

for ( $j=1; $j<$b; $j++ ) :
if ( substr ($this->chr2bin ($str[$i+$j]), 0, 2) != 10 )
return 0;
endfor;

break;
endfor;

return $_not ? 0 : 1;
}
}

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
?>

사용예
<?
require_once 'Unicode.php';
$_f = 'usage_200509.html';
$_ff = 'bb.html';

$u = new Unicode;
if ( file_exists ($_ff) )
unlink ($_ff);

$_t = file ($_f);

foreach ( $_t as $_v ) :
$_v = trim ($_v);
if ( $u->is_utf8 ($_v) )
putfile_lib ($_ff, utf8decode_lib ($_v, 'cp949'), 1);
else
putfile_lib ($_ff, $_v, 1);
endforeach;

?>

그외 참고 url
http://my.oops.org/126<div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]</div>
|

댓글 1개

내가 조아라 하는 pear소스네요...ㅎㅎ
댓글을 작성하시려면 로그인이 필요합니다. 로그인

개발자팁

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
MySQL 15년 전 조회 2,685
기타 15년 전 조회 2,166
Flash 15년 전 조회 2,009
PHP 15년 전 조회 2,333
기타 15년 전 조회 2,100
MySQL 15년 전 조회 4,634
Flash 15년 전 조회 2,014
기타 15년 전 조회 1,815
JavaScript 15년 전 조회 2,180
기타 15년 전 조회 2,045
기타 15년 전 조회 2,840
MySQL 15년 전 조회 2,364
기타 15년 전 조회 4,689
MySQL 15년 전 조회 2,600
기타 15년 전 조회 3,396
기타 15년 전 조회 2,287
Flash 15년 전 조회 3,095
기타 15년 전 조회 2,590
Flash 15년 전 조회 2,212
기타 15년 전 조회 1,969
기타 15년 전 조회 1.1만
기타 15년 전 조회 2,410
기타 15년 전 조회 3,350
Flash 15년 전 조회 8,076
기타 15년 전 조회 5,679
기타 15년 전 조회 2,484
JavaScript 15년 전 조회 1.2만
MySQL 15년 전 조회 8,833
Flash 15년 전 조회 2,241
기타 15년 전 조회 3,426
🐛 버그신고