한글 자음 모음(초성, 중성, 종성) 분리
질문답변 메뉴에서 질문이 있어 답을 드리고 유용할 듯 싶어 남겨봅니다.
저도 궁굼해서 찾아본 내용이고 필요한 내용이라 올려봅니다.
<?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 팁자료실
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 공지 | 3년 전 | 4401 | ||
| 2484 | 1년 전 | 1262 | ||
| 2483 | 1년 전 | 862 | ||
| 2482 | 1년 전 | 711 | ||
| 2481 | 1년 전 | 1082 | ||
| 2480 | 1년 전 | 1184 | ||
| 2479 | 1년 전 | 840 | ||
| 2478 | 1년 전 | 1177 | ||
| 2477 | 1년 전 | 780 | ||
| 2476 | 1년 전 | 1587 | ||
| 2475 | 1년 전 | 923 | ||
| 2474 |
|
1년 전 | 933 | |
| 2473 | 1년 전 | 1519 | ||
| 2472 | 1년 전 | 860 | ||
| 2471 | 1년 전 | 918 | ||
| 2470 | 1년 전 | 788 | ||
| 2469 | 1년 전 | 1372 | ||
| 2468 | 1년 전 | 2957 | ||
| 2467 | 1년 전 | 853 | ||
| 2466 |
|
1년 전 | 1480 | |
| 2465 | 1년 전 | 802 | ||
| 2464 | 1년 전 | 1180 | ||
| 2463 | 1년 전 | 1352 | ||
| 2462 | 1년 전 | 1146 | ||
| 2461 |
|
1년 전 | 1170 | |
| 2460 | 1년 전 | 759 | ||
| 2459 | 1년 전 | 948 | ||
| 2458 | 1년 전 | 1345 | ||
| 2457 | 1년 전 | 1267 | ||
| 2456 |
|
1년 전 | 770 | |
| 2455 |
블랙캣77
|
1년 전 | 1496 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기