Multibyte mb_detect_encoding
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
mb_detect_encoding — Detect character encoding
설명
mb_detect_encoding(string $string
, array|string|null $encodings
= null
, bool $strict
= false
): string|false
정렬된 후보 목록에서 문자열 string
에 대해 가장 가능성이 높은 문자 인코딩을 감지합니다.
의도한 문자 인코딩의 자동 감지는 결코 완전히 신뢰할 수 없습니다. 추가 정보가 없으면 키 없이 암호화된 문자열을 디코딩하는 것과 유사합니다. "Content-Type" HTTP 헤더와 같이 데이터와 함께 저장되거나 전송되는 문자 인코딩 표시를 사용하는 것이 항상 바람직합니다.
이 함수는 모든 바이트 시퀀스가 유효한 문자열을 형성하지 않는 멀티바이트 인코딩에서 가장 유용합니다. 입력 문자열에 이러한 시퀀스가 포함되어 있으면 해당 인코딩이 거부되고 다음 인코딩이 확인됩니다.
매개변수
string
- 검사 중인 문자열입니다.
encodings
- 순서대로 시도할 문자 인코딩 목록입니다. 목록은 문자열 배열로 지정하거나 쉼표로 구분된 단일 문자열로 지정할 수 있습니다.
encodings
이 생략되거나null
이면 현재 detect_order(mbstring.detect_order 구성 옵션 또는 mb_detect_order() 함수로 설정)가 사용됩니다. strict
- 나열된
encodings
에서string
이 유효하지 않은 경우 동작을 제어합니다.strict
가 false로 설정되면 가장 근접하게 일치하는 인코딩이 반환됩니다.strict
가true
로 설정되면false
가 반환됩니다.strict
의 기본값은 mbstring.strict_detection 구성 옵션으로 설정할 수 있습니다.
반환 값
감지된 문자 인코딩 또는 문자열이 나열된 인코딩에서 유효하지 않은 경우 false
입니다.
Examples
예제 #1 mb_detect_encoding() 예제
<?php
// Detect character encoding with current detect_order
echo mb_detect_encoding($str);
// "auto" is expanded according to mbstring.language
echo mb_detect_encoding($str, "auto");
// Specify "encodings" parameter by list separated by comma
echo mb_detect_encoding($str, "JIS, eucjp-win, sjis-win");
// Use array to specify "encodings" parameter
$encodings = [
"ASCII",
"JIS",
"EUC-JP"
];
echo mb_detect_encoding($str, $encodings);
?>
예제 #2 strict
매개변수의 영향
<?php
// 'áéóú' encoded in ISO-8859-1
$str = "\xE1\xE9\xF3\xFA";
// The string is not valid ASCII or UTF-8, but UTF-8 is considered a closer match
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8'], false));
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8'], true));
// If a valid encoding is found, the strict parameter does not change the result
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8', 'ISO-8859-1'], false));
var_dump(mb_detect_encoding($str, ['ASCII', 'UTF-8', 'ISO-8859-1'], true));
?>
위의 예는 다음을 출력합니다.
string(5) "UTF-8" bool(false) string(10) "ISO-8859-1" string(10) "ISO-8859-1"
경우에 따라 동일한 바이트 시퀀스가 여러 문자 인코딩에서 유효한 문자열을 형성할 수 있으며 어떤 해석이 의도되었는지 알 수 없습니다. 예를 들어, 바이트 시퀀스 "\xC4\xA2"는 다음과 같을 수 있습니다.
- ISO-8859-1, ISO-8859-15 또는 Windows-1252 중 하나로 인코딩된 "Ä¢"(U+00C4 라틴 대문자 A와 DIAERESIS 뒤에 U+00A2 CENT SIGN)
- ISO-8859-5로 인코딩된 "ФЂ"(U + 0424 CYRILLIC CAPITAL LETTER EF 다음에 U + 0402 CYRILLIC CAPITAL LETTER DJE)
- UTF-8로 인코딩된 "Ģ"(U + 0122 LATIN CAPITAL LETTER G WITH CEDILLA)
예제 #3 여러 인코딩이 일치할 때 순서의 영향
<?php
$str = "\xC4\xA2";
// The string is valid in all three encodings, so the first one listed will be returned
var_dump(mb_detect_encoding($str, ['UTF-8', 'ISO-8859-1', 'ISO-8859-5']));
var_dump(mb_detect_encoding($str, ['ISO-8859-1', 'ISO-8859-5', 'UTF-8']));
var_dump(mb_detect_encoding($str, ['ISO-8859-5', 'UTF-8', 'ISO-8859-1']));
?>
위의 예는 다음을 출력합니다.
string(5) "UTF-8" string(10) "ISO-8859-1" string(10) "ISO-8859-5"
기타
- mb_detect_order() - 문자 인코딩 감지 순서 설정/가져오기