php에서 '특정 문자열'만 인코딩 방식이 엉뚱하게 검사됩니다.

wjddndyd401의 이미지

파일에 들어있는 개인정보를 DB에 업로드하는 php 페이지가 있습니다.

업로드되는 파일이 UTF-8일수도 있고 EUC-KR일수도 있어서
인코딩 방식을 검사한 후 그에 맞게 인코딩해서 DB에 업로드합니다.
검사에는 iconv와 mb_detect_encoding 함수를 이용했습니다.

헌데 두 방식을 다 사용해봤는데
파일 내용이 EUC-KR일 때
정말 특정 문자열에 한해서만 UTF-8로 알아먹습니다. 당연히 인코딩이 제대로 되지 않구요.

임시로 페이지를 만들어서 iconv를 이용해서 강제로 EUC-KR로 인코딩을 해봤는데
분명히 EUC-KR로 인코딩한 결과물을 검사해봤는데도 결과가 UTF-8로 나옵니다.

그 특정 문자열이 한두개는 아닌 것 같고, 지금 확인되는 예시는 '황치호'라는 이름이 있습니다.
아무리 EUC-KR로 인코딩한 다음 검사해도 UTF-8로 결과가 나옵니다.

혹시 이 문제를 경험해보신 분이 있는지,
해결법이나 우회법이 있는지 궁금합니다.

pynoos의 이미지

너무 샘플 길이가 작아서 생기는 문제 같습니다. 다른 검출기를 사용하시는 것이 좋을 것 같은데요?

http://my.oops.org/126

이 글이 참고가 되시려나...

peecky의 이미지

EUC-KR로 '황치호'라는 값이 저장되어 있으면,
c8 b2 c4 a1 c8 a3
라는 값을 가지게 되고, 이를 UTF-8로 읽을 경우 'Ȳġȣ'라는 올바른(!) UTF-8 문자열이 됩니다.

만약 입력의 내용이 다국어가 아닌 한글, 영문, 숫자 등에 국한된다면, mb_detect_encoding()이 UTF-8로 판별하더라도, 정규식으로 의도하지 않은 문자가 포함되어있는지 한번 더 체크해보면 될 듯 합니다.

function detectEncoding($input) {
	$detected_encoding1 = mb_detect_encoding($input, ['EUC-KR', 'UTF-8']);
	$detected_encoding2 = mb_detect_encoding($input, ['UTF-8', 'EUC-KR']);
 
	if ($detected_encoding1 === $detected_encoding2) $detected_encoding = $detected_encoding1;
	else {
		$pattern = '/\A[\x{20}-\x{7E}\x{AC00}-\x{D7A3}]*\z/u'; // ascii characters and Hangul characters('가' to '힣')
		if (preg_match($pattern, iconv($detected_encoding1, 'UTF-8', $input))) $detected_encoding = $detected_encoding1;
		else $detected_encoding = 'UTF-8';
	}
 
	return $detected_encoding;
}
 
function detectEncodingAndPrint($input) {
	$detected_encoding = detectEncoding($input);
	echo $detected_encoding;
	if ($detected_encoding === 'UTF-8') echo $input;
	else echo iconv($detected_encoding, 'UTF-8', $input);
	echo "\n";
}
 
detectEncodingAndPrint('황치호');
detectEncodingAndPrint('Ȳġȣ');
wjddndyd401의 이미지

말씀대로 입력값은 한글, 영문자, 숫자 뿐이라 올려주신 코드로 해결했습니다. 정규식 공부 좀 해야겠네요.
그 와중에 iconv 파라미터를 죄다 소문자로 넣어 놔서 한바탕 헤맨 것은 덤....

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.