"PHP 문자가 한글인지 확인 is hangul char()"의 두 판 사이의 차이

17번째 줄: 17번째 줄:
==예제==
==예제==
<syntaxhighlight lang='php' run>
<syntaxhighlight lang='php' run>
function utf8_ord($ch) {
function utf8_ord($c) {
   $len = strlen($ch);
   $len = strlen($c);
   if($len <= 0) return false;
   if($len <= 0) return false;
   $h = ord($ch{0});
   $h = ord($c[0]);
   if ($h <= 0x7F) return $h;
   if ($h <= 0x7F) return $h;
   if ($h < 0xC2) return false;
   if ($h < 0xC2) return false;
   if ($h<=0xDF && $len>1) return ($h & 0x1F) << 6 | (ord($ch{1}) & 0x3F);
   if ($h<=0xDF && $len>1) return ($h & 0x1F) << 6 | (ord($c[1]) & 0x3F);
   if ($h<=0xEF && $len>2) return ($h & 0x0F) << 12 | (ord($ch{1}) & 0x3F) << 6 | (ord($ch{2}) & 0x3F);
   if ($h<=0xEF && $len>2) return ($h & 0x0F) << 12 | (ord($c[1]) & 0x3F) << 6 | (ord($c[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);
   if ($h<=0xF4 && $len>3) return ($h & 0x0F) << 18 | (ord($c[1]) & 0x3F) << 12 | (ord($c[2]) & 0x3F) << 6 | (ord($c[3]) & 0x3F);
   return false;
   return false;
}
}


function is_hangul_char($ch) {
function is_hangul_char($c) {
   $c = utf8_ord($ch);
   $o = utf8_ord($c);
   if( 0x1100<=$c && $c<=0x11FF ) return true;
   if( 0x1100<=$o && $o<=0x11FF ) return true;
   if( 0x3130<=$c && $c<=0x318F ) return true;
   if( 0x3130<=$o && $o<=0x318F ) return true;
   if( 0xAC00<=$c && $c<=0xD7A3 ) return true;
   if( 0xAC00<=$o && $o<=0xD7A3 ) return true;
   return false;
   return false;
}
}


function test($ch) {
function test($c) {
   echo "<br>[$ch] is ";
   echo "<br>[$c] is ";
   if(is_hangul_char($ch)) echo "hangul.";
   if(is_hangul_char($c)) echo "hangul.";
   else echo "not hangul.";
   else echo "not hangul.";
}
}
45번째 줄: 45번째 줄:
test('A');  # [A] is not hangul.
test('A');  # [A] is not hangul.
test(',');  # [,] is not hangul.
test(',');  # [,] is not hangul.
test('★');   # [★] is not hangul.
test('★'); # [★] is not hangul.
test('가');   # [가] is hangul.
test('가'); # [가] is hangul.
test('ㄱ');   # [ㄱ] is hangul.
test('ㄱ'); # [ㄱ] is hangul.
test('ㅏ');   # [ㅏ] is hangul.
test('ㅏ'); # [ㅏ] is hangul.
test('힣');   # [힣] is hangul.
test('힣'); # [힣] is hangul.
test('日');  # [日] is not hangul.
test('日');  # [日] is not hangul.
test('に');  # [に] is not hangul.
test('に');  # [に] is not hangul.

2021년 4월 15일 (목) 23:25 판

함수 is_hangul_char()
PHP에서 문자가 한글인지 확인
PHP 한글인지 확인

1 핵심 코드

function is_hangul_char($ch) {
  $c = utf8_ord($ch);
  if( 0x1100<=$c && $c<=0x11FF ) return true;
  if( 0x3130<=$c && $c<=0x318F ) return true;
  if( 0xAC00<=$c && $c<=0xD7A3 ) return true;
  return false;
}

2 예제

function utf8_ord($c) {
  $len = strlen($c);
  if($len <= 0) return false;
  $h = ord($c[0]);
  if ($h <= 0x7F) return $h;
  if ($h < 0xC2) return false;
  if ($h<=0xDF && $len>1) return ($h & 0x1F) << 6 | (ord($c[1]) & 0x3F);
  if ($h<=0xEF && $len>2) return ($h & 0x0F) << 12 | (ord($c[1]) & 0x3F) << 6 | (ord($c[2]) & 0x3F);
  if ($h<=0xF4 && $len>3) return ($h & 0x0F) << 18 | (ord($c[1]) & 0x3F) << 12 | (ord($c[2]) & 0x3F) << 6 | (ord($c[3]) & 0x3F);
  return false;
}

function is_hangul_char($c) {
  $o = utf8_ord($c);
  if( 0x1100<=$o && $o<=0x11FF ) return true;
  if( 0x3130<=$o && $o<=0x318F ) return true;
  if( 0xAC00<=$o && $o<=0xD7A3 ) return true;
  return false;
}

function test($c) {
  echo "<br>[$c] is ";
  if(is_hangul_char($c)) echo "hangul.";
  else echo "not hangul.";
}

test('A');   # [A] is not hangul.
test(',');   # [,] is not hangul.
test('★');  # [★] is not hangul.
test('가');  # [가] is hangul.
test('ㄱ');  # [ㄱ] is hangul.
test('ㅏ');  # [ㅏ] is hangul.
test('힣');  # [힣] is hangul.
test('日');  # [日] is not hangul.
test('に');  # [に] is not hangul.

3 같이 보기

4 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}