HOME   로그인   회원가입
    
회원가입
비밀번호 찾기 자동로긴
이전게시판
   free_board
   건강게시판
   오라클DB
   Linux
   HTML/javascript
   질문과 답
호서기
   이미지게시판  
   건강게시판  
   음악자료  
   성준이 게시판  
  2010-06-02 20:24:132215 
converts a UTF8-string into HTML entities
호석
일반
silverbeat -eat- gmx -hot- at
10-Mar-2010 05:42
When using UTF-8 as a charset, htmlentities will only convert 1-byte and 2-byte characters. Use this function if you also want to convert 3-byte and 4-byte characters:

<?php

// converts a UTF8-string into HTML entities
//  - $utf8:        the UTF8-string to convert
//  - $encodeTags:  booloean. TRUE will convert "<" to "<"
//  - return:       returns the converted HTML-string
function utf8tohtml($utf8, $encodeTags) {
    $result = '';
    for ($i = 0; $i < strlen($utf8); $i++) {
        $char = $utf8[$i];
        $ascii = ord($char);
        if ($ascii < 128) {
            // one-byte character
            $result .= ($encodeTags) ? htmlentities($char) : $char;
        } else if ($ascii < 192) {
            // non-utf8 character or not a start byte
        } else if ($ascii < 224) {
            // two-byte character
            $result .= htmlentities(substr($utf8, $i, 2), ENT_QUOTES, 'UTF-8');
            $i++;
        } else if ($ascii < 240) {
            // three-byte character
            $ascii1 = ord($utf8[$i+1]);
            $ascii2 = ord($utf8[$i+2]);
            $unicode = (15 & $ascii) * 4096 +
                       (63 & $ascii1) * 64 +
                       (63 & $ascii2);
            $result .= "&#$unicode;";
            $i += 2;
        } else if ($ascii < 248) {
            // four-byte character
            $ascii1 = ord($utf8[$i+1]);
            $ascii2 = ord($utf8[$i+2]);
            $ascii3 = ord($utf8[$i+3]);
            $unicode = (15 & $ascii) * 262144 +
                       (63 & $ascii1) * 4096 +
                       (63 & $ascii2) * 64 +
                       (63 & $ascii3);
            $result .= "&#$unicode;";
            $i += 3;
        }
    }
    return $result;
}

echo utf8tohtml($anyUTF8string, TRUE);

?>

regards, silverbeat
목록
4814
번호 분류 제목 성명 날짜 읽음
48 일반 jQuery를 배울수 있는 동네들입니다. 호석 14-03-03 2917
47 일반 PRO*C XML 생성 처리관련 호석 13-03-07 2322
46 일반 jquery 아작스 종합정리 (1) 호석 12-08-17 2742
45 일반 자바스크립트 특수문자 체크로직 goood 호석 12-03-30 2409
44 일반 JSP에서 iBATIS Framework 사용하기 호석 12-03-09 2606
43 일반 웹 개발자를 위한 jQuery 기본이해 호석 12-02-14 2404
42 일반 jQuery에 대해서 첫줄에 내포되어 있는의미 호석 11-08-04 2298
41 일반 메뉴컨트롤이 언급되어 있는곳 호석 11-08-04 2427
40 일반 Ckeditor 설치 및 설정 (1) 호석 11-08-04 2475
39 일반 HTML5의 모든 것 호석 11-07-06 2043
38 일반 CURL 을 이용한 GET/POST 변수 전송 (2) 호석 11-06-21 2502
37 일반 jquery 에니메이트 그래프 good 좋아요 호석 11-06-18 2433
36 일반 jjQuery Plugin Graph (그래프 플러그인) (1) 호석 11-05-27 2476
일반 converts a UTF8-string into HTML entities 호석 10-06-02 2216
34 일반 getElementById와 getElementByName의 차이 호석 10-02-18 3154
1 [2] [3] [4]  다음맨끝
 
Copyright © zenos.pe.kr. All rights reserved.