한글 자음 모음(초성, 중성, 종성) 분리
질문답변 메뉴에서 질문이 있어 답을 드리고 유용할 듯 싶어 남겨봅니다.
저도 궁굼해서 찾아본 내용이고 필요한 내용이라 올려봅니다.
<?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 hangul_separa($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 hangul_separa("초성 중성 종성 분리");
// => ㅊㅗㅅㅓㅇ ㅈㅜㅇㅅㅓㅇ ㅈㅗㅇㅅㅓㅇ ㅂㅜㄴㄹㅣ
?>
저도 궁굼해서 찾아본 내용이고 필요한 내용이라 올려봅니다.
<?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 hangul_separa($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 hangul_separa("초성 중성 종성 분리");
// => ㅊㅗㅅㅓㅇ ㅈㅜㅇㅅㅓㅇ ㅈㅗㅇㅅㅓㅇ ㅂㅜㄴㄹㅣ
?>
게시판 목록
그누보드5 팁자료실
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 2455 |
|
1년 전 | 1118 | |
| 2454 | 1년 전 | 1053 | ||
| 2453 | 1년 전 | 1618 | ||
| 2452 | 1년 전 | 1152 | ||
| 2451 | 1년 전 | 955 | ||
| 2450 | 1년 전 | 1333 | ||
| 2449 | 1년 전 | 881 | ||
| 2448 | 1년 전 | 1331 | ||
| 2447 | 1년 전 | 1410 | ||
| 2446 | 1년 전 | 1125 | ||
| 2445 | 1년 전 | 1281 | ||
| 2444 | 1년 전 | 1674 | ||
| 2443 | 1년 전 | 1483 | ||
| 2442 | 1년 전 | 1070 | ||
| 2441 | 1년 전 | 1176 | ||
| 2440 | 1년 전 | 4619 | ||
| 2439 | 1년 전 | 1075 | ||
| 2438 | 1년 전 | 1115 | ||
| 2437 | 1년 전 | 925 | ||
| 2436 | 1년 전 | 1557 | ||
| 2435 | 1년 전 | 1453 | ||
| 2434 | 1년 전 | 1016 | ||
| 2433 |
|
1년 전 | 613263 | |
| 2432 |
|
1년 전 | 1091 | |
| 2431 |
|
1년 전 | 1705 | |
| 2430 | 1년 전 | 1310 | ||
| 2429 | 1년 전 | 1269 | ||
| 2428 | 1년 전 | 1145 | ||
| 2427 | 1년 전 | 1108 | ||
| 2426 |
뽕엄능브라
|
1년 전 | 1229 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기