php 문자열 한글여부 확인

2022. 4. 22. 16:11Dev

반응형
<?
  $id = 'abc';
  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;
  }
  //var_dump(is_hangul_char($idid));

  if(is_hangul_char($id)==true){
	  echo 1;
  }else{
	  echo 2;
  }


?>
반응형