가끔 개발과정에 디비에 저장한 텍스트가 페이지에서는 깨진 글처럼 보일 때가 있다.
이것은 UTF8을 제대로 구현하지 못한것을 의미한다.
이번 글에서는 PHP, HTML 쪽에서 이문제를 어떻게 해결 하는지 샘플코드로 설명하려고 한다.
1. 입력값에 대한 UTF8설정
PHP에서 클라이언트쪽에서 요청보내어진 문자열을 정확히 UTF8처리하려면
[code]
$string = $_REQUEST['user_comment'];
if (!mb_check_encoding($string, 'UTF-8')) {
// 문자열이 UTF8인코딩이 아니면 UTF8로 인코딩한다.
$actualEncoding = mb_detect_encoding($string);
$string = mb_convert_encoding($string, 'UTF-8', $actualEncoding);
// $string 을 디비에 저장 또는 그어떤 로직 실행....
}
[/code]
만일 웹페이지에서 form태그로 서버에 값을 넘긴다면
[code]
<form action="somepage.php" accept-charset="UTF-8">
[/code]
이렇게 해주는것이 좋다.
2. 출력값에 대한 UTF8
PHP에서 직접 UTF8인코딩 헤더를 작성해줄수 있다.
[code]
header('Content-Type: text/html; charset=utf-8');
[/code]
만일 출력을 HTML페이지로 한다면
[code]
HTML5
<meta charset="utf-8">
낮은 버전에서는
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
[/code]
3. 디비에서 UTF8
디비의 charset, collation을 utf8mb4(MySQL < 5.5.3 은 utf8) 로 설정해주어야 한다.
게시글 목록
| 번호 | 제목 |
|---|---|
| 15969 |
jQuery
.jquery
|
| 15968 |
JavaScript
공휴일을 제외한 시간적용 ON/OFF 스크립트예제 (휴일추가가능)
|
| 15967 |
jQuery
.is()
|
| 15966 |
jQuery
.insertBefore()
|
| 15965 |
jQuery
.insertAfter()
|
| 15964 |
jQuery
.innerWidth ()
|
| 15963 |
jQuery
.innerHeight ()
|
| 15962 |
jQuery
.index()
|
| 15961 |
jQuery
jQuery( ":image" )
|
| 15960 |
jQuery
jQuery ( "# id")
|
| 15959 |
jQuery
.html()
|
| 15958 | |
| 15957 |
jQuery
.hide ()
|
| 15956 |
jQuery
jQuery ( ": hidden")
|
| 15955 |
기타
그누보드5 기본쿼리
|
| 15954 |
jQuery
.height()
|
| 15953 |
jQuery
jQuery ( ": header")
|
| 15952 |
jQuery
.hasClass( className )
|
| 15949 |
PHP
네이버 클로버 tts
3
|
| 15947 |
node.js
네이트온 팀룸으로 메일 수신 확인
1
|
| 15946 |
jQuery
jQuery ( ": has (selector)")
|
| 15945 |
jQuery
attributeHas 선택자
|
| 15944 |
jQuery
.has (selector)
|
| 15942 |
jQuery
.get (index)
|
| 15941 |
jQuery
.focusin (handler)
|
| 15940 |
jQuery
.focusin (handler)
|
| 15939 |
jQuery
jQuery ( ": focus")
|
| 15938 |
jQuery
.focus (handler)
|
| 15937 |
jQuery
jQuery( ":first" )
|
| 15936 |
jQuery
jQuery ( ": first-of-type")
|
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기