Live Brilliant

[PHP] 초성 추출 소스 본문

개발은 핵찜이야/PHP

[PHP] 초성 추출 소스

주인정 2013. 1. 25. 18:35

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($row[name]);


[출처] http://jmnote.com/wiki/UTF-8_%ED%95%9C%EA%B8%80_%EC%B4%88%EC%84%B1_%EC%B6%94%EC%B6%9C_(PHP)

Comments