UTF-8 한글 TEST 정보
JavaScript UTF-8 한글 TEST
본문
$str = "한글 abc 123 사랑합니다.";
$str = preg_replace("#\r|\n#", " ", trim($str));
$len = strlen($str);
$i = 0;
$k = 0;
while ($i < $len) {
if (preg_match('/^[\x20]/xs', $str)) {
echo $str[0];
$n = 1;
}
else if (preg_match('/^[\x30-\x39\x41-\x5a\x61-\x7a]/xs', $str)) {
echo $str[0];
$n = 1;
}
else if (preg_match('/^[\xc0-\\xdf][\x80-\xbf]/xs', $str)) {
echo $str[0].$str[1];
$n = 2;
}
else if (preg_match('/^[\xe0-\\xef][\x80-\xbf]{2}/xs', $str)) {
echo $str[0].$str[1].$str[2];
$n = 3;
}
else if (preg_match('/^[\xf0-\\xf7][\x80-\xbf]{3}/xs', $str)) {
echo $str[0].$str[1].$str[2].$str[3];
$n = 4;
}
else {
$n = 1;
}
$str = substr($str, $n);
$i += $n;
//echo $i; echo "<br>";
if ($k++ > 10000) break;
}
0
댓글 1개

bytes bits representation
1 7 0bbbbbbb
2 11 110bbbbb 10bbbbbb
3 16 1110bbbb 10bbbbbb 10bbbbbb
4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
예를 들어 3바이트(한글 포함) UTF-8 의 경우
E0-EF 에 포함되고 뒤 이은 2바이트가 80-BF 에 포함되면 UTF-8 한글임