- 정보공유
[PHP] 한글 초성 추출하기
- 작성일
- 17-03-30 23:18
- 조회수
- 35,485 건
- 댓글
- 0 건
http://zetawiki.com/wiki/UTF-8_%ED%95%9C%EA%B8%80_%EC%B4%88%EC%84%B1_%…10224회 연결
<?php
function utf8_strlen($str) { return mb_strlen($str, 'UTF-8'); }
function utf8_charAt($str, $num) { return mb_substr($str, $num, 1, 'UTF-8'); }
function utf8_ord($ch) {
$len = strlen($ch);
if($len <= 0) return false;
$h = ord($ch{0});
if ($h <= 0x7F) return $h;
if ($h < 0xC2) return false;
if ($h <= 0xDF && $len>1) return ($h & 0x1F) << 6 | (ord($ch{1}) & 0x3F);
if ($h <= 0xEF && $len>2) return ($h & 0x0F) << 12 | (ord($ch{1}) & 0x3F) << 6 | (ord($ch{2}) & 0x3F);
if ($h <= 0xF4 && $len>3) return ($h & 0x0F) << 18 | (ord($ch{1}) & 0x3F) << 12 | (ord($ch{2}) & 0x3F) << 6 | (ord($ch{3}) & 0x3F);
return false;
}
function cho_hangul($str) {
$cho = array("ㄱ","ㄲ","ㄴ","ㄷ","ㄸ","ㄹ","ㅁ","ㅂ","ㅃ","ㅅ","ㅆ","ㅇ","ㅈ","ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ");
$result = "";
for ($i=0; $i<utf8_strlen($str); $i++) {
$code = utf8_ord(utf8_charAt($str, $i)) - 44032;
if ($code > -1 && $code < 11172) {
$cho_idx = $code / 588;
$result .= $cho[$cho_idx];
}
}
return $result;
}
echo cho_hangul("안녕하세요");