메뉴 건너뛰기

프로그램언어

2015.04.14 19:12

한글자르기 substr

조회 수 25198 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

완성형 한글의 경우 한글은 2바이트로 영어는 1바이트로 구성되기 때문에 PHP의 substr() 함수를 쓸 때 한글이 깨지는 경우가 있다. 이런 경우 mb_substr() 함수로 대체하면 된다.


substr ($str, $start_Pos, $length);
$Str에 담고 있는 문자열의 $start_Pos부터 길이$length만큼을 잘라내어 반환한다

mb_substr ($str, $start_Pos, $length, $encoding);
$str에 담고 있는 문자열의 $start_Pos부터 길이$length만큼을 잘라내어 $encoding 형식에 맞게 반환한다


mb_substr($str, 0, 10, 'EUC-KR');
mb_substr($str, 0, 10, 'UTF-8');

function strcut_euckr($msg, $limit) {    // 완성형 한글 자르기
    $msg = substr($msg, 0, $limit);
    for ($i = $limit - 1; $i > 1; $i--) {   
        if (ord(substr($msg,$i,1)) < 128) break;
    }
    $msg = substr($msg, 0, $limit - ($limit - $i + 1) % 2);
    return $msg;
}


function strcut_utf8($str, $len, $checkmb=false, $tail='') {  // UTF-8 한글자르기
    /** UTF-8 Format
    * 0xxxxxxx = ASCII, 110xxxxx 10xxxxxx or 1110xxxx 10xxxxxx 10xxxxxx
    * latin, greek, cyrillic, coptic, armenian, hebrew, arab characters consist of 2bytes
    * BMP(Basic Mulitilingual Plane) including Hangul, Japanese consist of 3bytes
    **/
    preg_match_all('/[\xE0-\xFF][\x80-\xFF]{2}|./', $str, $match); // target for BMP
     
    $m = $match[0];
    $slen = strlen($str); // length of source string
    $tlen = strlen($tail); // length of tail string
    $mlen = count($m); // length of matched characters
     
    if ($slen <= $len) return $str;
    if (!$checkmb && $mlen <= $len) return $str;
     
    $ret = array();
    $count = 0;
    for ($i=0; $i < $len; $i++) {
        $count += ($checkmb && strlen($m[$i]) > 1)?2:1;
        if ($count + $tlen > $len) break;
        $ret[] = $m[$i];
    }    
    return join('', $ret).$tail;
}



  1. No Image 12Apr
    by
    2014/04/12 Views 26546 

    한글줄바꾸기 또는 utf-8 wordwrap

  2. No Image 27Feb
    by
    2014/02/27 Views 26351 

    해당하는 날짜가 그달의 몇주째인지 계산

  3. No Image 07Mar
    by
    2017/03/07 Views 26319 

    php 세션 유지시간 늘리기

  4. No Image 27Feb
    by
    2014/02/27 Views 26046 

    페이지 로딩 시간 측정

  5. No Image 21Sep
    by
    2016/09/21 Views 25959 

    환경변수 HTTP_USER_AGENT를 이용해서 스마트 기기 분류하기

  6. 디렉토리내 특정 확장자를 가진 파일 전부 삭제

  7. No Image 27Feb
    by
    2014/02/27 Views 25438 

    로또 숫자 랜덤하게 1~45까지 숫자 빼오기

  8. No Image 06Apr
    by
    2015/04/06 Views 25426 

    홈페이지 귀퉁이에 붙이는 공지창

  9. No Image 14Apr
    by
    2015/04/14 Views 25334 

    파일 삭제

  10. No Image 14Apr
    by 조쉬
    2015/04/14 Views 25198 

    한글자르기 substr

  11. No Image 14Apr
    by
    2015/04/14 Views 24904 

    array (배열)

  12. No Image 07Mar
    by
    2017/03/07 Views 24849 

    기본적인 페이징

  13. No Image 27Feb
    by
    2014/02/27 Views 24444 

    $_SERVER변수

  14. No Image 27Feb
    by
    2014/02/27 Views 24276 

    필드값 저장

  15. No Image 14Apr
    by
    2015/04/14 Views 24250 

    addslashes 함수의 필요성

  16. No Image 23Dec
    by
    2016/12/23 Views 23943 

    $_SERVER 함수

  17. No Image 23Dec
    by
    2016/12/23 Views 23847 

    $_FILES

  18. No Image 25Mar
    by
    2015/03/25 Views 23347 

    [PHP] POST 로 넘어온 모든 변수값 확인하기

  19. POST, GET으로 배열값 받기(직렬화)

  20. ajax refresh 시키기(자동리플래쉬) with php

Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved