PHP 한글 초성, 중성, 종성 분리

페이지 정보

profile_image
작성자 LJM
댓글 0건 조회 47회 작성일 18-10-10 21:44

본문

<?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 linear_hangul($str) {

$cho = array("ㄱ","ㄲ","ㄴ","ㄷ","ㄸ","ㄹ","ㅁ","ㅂ","ㅃ","ㅅ","ㅆ","ㅇ","ㅈ","ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ");

$jung = array("ㅏ","ㅐ","ㅑ","ㅒ","ㅓ","ㅔ","ㅕ","ㅖ","ㅗ","ㅘ","ㅙ","ㅚ","ㅛ","ㅜ","ㅝ","ㅞ","ㅟ","ㅠ","ㅡ","ㅢ","ㅣ");

$jong = 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;

  $jung_idx = $code % 588 / 28;

$jong_idx = $code % 28;

$result .= $cho[$cho_idx].$jung[$jung_idx].$jong[$jong_idx];

} else {

$result .= utf8_charAt($str, $i);

}

}

return $result;

}

 

echo linear_hangul("안녕하세요");

// ㅇㅏㄴㄴㅕㅇㅎㅏㅅㅔㅇㅛ

?>

댓글목록

등록된 댓글이 없습니다.

로그인