메뉴 건너뛰기

프로그램언어

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. 시간관련함수

    Date2016.12.23 Views17328
    Read More
  2. PHP Mcrypt 라이브러리를 활용한 암호화 시스템

    Date2016.12.22 Views19972
    Read More
  3. $_SERVER 환경변수

    Date2016.09.21 Views33237
    Read More
  4. 환경변수 HTTP_USER_AGENT를 이용해서 스마트 기기 분류하기

    Date2016.09.21 Views25959
    Read More
  5. 템플릿 관련 정보

    Date2016.08.22 Views20732
    Read More
  6. RSSReader Class 제작 및 Reader 만들기

    Date2016.08.22 Views21042
    Read More
  7. HTTP Protocol의 data method - GET / POST

    Date2016.04.22 Views20637
    Read More
  8. PHP에서 CSV 파일 export

    Date2016.04.22 Views22335
    Read More
  9. 디렉토리내 특정 확장자를 가진 파일 전부 삭제

    Date2015.04.14 Views25515
    Read More
  10. PHP error 메시지 출력

    Date2015.04.14 Views20924
    Read More
  11. 검색어 처리 루틴

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

    Date2015.04.14 Views25334
    Read More
  13. PHP 삼항연산자 ?

    Date2015.04.14 Views22947
    Read More
  14. PHP continue 문

    Date2015.04.14 Views21097
    Read More
  15. array (배열)

    Date2015.04.14 Views24904
    Read More
  16. PHP switch 문

    Date2015.04.14 Views20993
    Read More
  17. 전화번호에 하이픈(-) 넣기

    Date2015.04.14 Views26622
    Read More
  18. 한글자르기 substr

    Date2015.04.14 Views25198
    Read More
  19. DB 내용을 화면에 출력(이중 for 문)

    Date2015.04.14 Views21246
    Read More
  20. PHP 파일 다루기

    Date2015.04.14 Views22401
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved