메뉴 건너뛰기

프로그램언어

2015.04.14 19:12

한글자르기 substr

조회 수 25206 추천 수 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. ajax refresh 시키기(자동리플래쉬) with php

    Date2017.03.06 Views23188
    Read More
  2. POST, GET으로 배열값 받기(직렬화)

    Date2017.03.06 Views23296
    Read More
  3. [PHP] POST 로 넘어온 모든 변수값 확인하기

    Date2015.03.25 Views23349
    Read More
  4. $_FILES

    Date2016.12.23 Views23851
    Read More
  5. $_SERVER 함수

    Date2016.12.23 Views23948
    Read More
  6. addslashes 함수의 필요성

    Date2015.04.14 Views24258
    Read More
  7. 필드값 저장

    Date2014.02.27 Views24284
    Read More
  8. $_SERVER변수

    Date2014.02.27 Views24454
    Read More
  9. 기본적인 페이징

    Date2017.03.07 Views24853
    Read More
  10. array (배열)

    Date2015.04.14 Views24908
    Read More
  11. 한글자르기 substr

    Date2015.04.14 Views25206
    Read More
  12. 파일 삭제

    Date2015.04.14 Views25336
    Read More
  13. 홈페이지 귀퉁이에 붙이는 공지창

    Date2015.04.06 Views25430
    Read More
  14. 로또 숫자 랜덤하게 1~45까지 숫자 빼오기

    Date2014.02.27 Views25441
    Read More
  15. 디렉토리내 특정 확장자를 가진 파일 전부 삭제

    Date2015.04.14 Views25519
    Read More
  16. 환경변수 HTTP_USER_AGENT를 이용해서 스마트 기기 분류하기

    Date2016.09.21 Views25961
    Read More
  17. 페이지 로딩 시간 측정

    Date2014.02.27 Views26048
    Read More
  18. php 세션 유지시간 늘리기

    Date2017.03.07 Views26342
    Read More
  19. 해당하는 날짜가 그달의 몇주째인지 계산

    Date2014.02.27 Views26353
    Read More
  20. 한글줄바꾸기 또는 utf-8 wordwrap

    Date2014.04.12 Views26547
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved