한글 자음 모음(초성, 중성, 종성) 분리
질문답변 메뉴에서 질문이 있어 답을 드리고 유용할 듯 싶어 남겨봅니다.
저도 궁굼해서 찾아본 내용이고 필요한 내용이라 올려봅니다.
<?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년 전 | 4402 | ||
| 654 | 8년 전 | 5460 | ||
| 653 | 8년 전 | 27675 | ||
| 652 | 8년 전 | 7459 | ||
| 651 | 8년 전 | 5776 | ||
| 650 | 8년 전 | 7782 | ||
| 649 | 8년 전 | 7265 | ||
| 648 | 8년 전 | 4343 | ||
| 647 | 8년 전 | 5613 | ||
| 646 | 8년 전 | 6824 | ||
| 645 | 8년 전 | 6539 | ||
| 644 |
|
8년 전 | 6074 | |
| 643 | 8년 전 | 5355 | ||
| 642 | 8년 전 | 5356 | ||
| 641 | 9년 전 | 7996 | ||
| 640 | 9년 전 | 6039 | ||
| 639 | 9년 전 | 4378 | ||
| 638 | 9년 전 | 8224 | ||
| 637 | 9년 전 | 8193 | ||
| 636 | 9년 전 | 6038 | ||
| 635 | 9년 전 | 15956 | ||
| 634 |
KeepItSimpleStyle
|
9년 전 | 5327 | |
| 633 | 9년 전 | 12140 | ||
| 632 | 9년 전 | 9126 | ||
| 631 |
minders
|
9년 전 | 10393 | |
| 630 | 9년 전 | 6694 | ||
| 629 | 9년 전 | 4575 | ||
| 628 | 9년 전 | 5084 | ||
| 627 | 9년 전 | 5396 | ||
| 626 | 9년 전 | 7888 | ||
| 625 | 9년 전 | 17703 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기