Contents

Develop
2015.11.10 15:51

[php] 한글 문자열 자르기 (utf-8)

Views 1315 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

그누보드/lib/common.lib.php line 890 

// 한글 한글자(2byte)는 길이 2, 공란.영숫자.특수문자는 길이 1 

function cut_str($str, $len, $suffix="…") 
{
    // 여기 추가,
    $s = iconv_substr($str, 0, $len, "utf-8");

    // 아래 주석처리 ! 
    //$s = substr($str, 0, $len); 
    //$cnt = 0; 
    //for ($i=0; $i<strlen($s); $i++) 
    //    if (ord($s[$i]) > 127) 
    //        $cnt++; 
    //$s = substr($s, 0, $len - ($cnt % 2)); 
    
    if (strlen($s) >= strlen($str))
        $suffix = ""; 
        
    return $s . $suffix; 
}

 

* 참고: PHP5에 iconv_substr( string str, int offset [, int length [, string charset]] ) 내장되었네요. 
* utf-8 적용시 cut_str()으로 잘 안될 때가 있어서 수정해봤습니다.


?

List of Articles
No. Category Subject Author Date Views
769 Develop [ios] 로컬에 있는 JS 파일 웹뷰에서 동적으로 실행하기 hooni 2015.02.10 947
768 Develop [ios] 상위 ViewController 가져오기 hooni 2015.10.12 951
767 Develop [ios] 오브젝티브C→스위프트, 코드 변환 손쉽게 file hooni 2015.08.07 955
766 Develop [ios] APNS, Remote Push 수신 시점에서 앱의 3가지 실행 상태 hooni 2018.10.19 963
765 Develop '2014 모바일 개발 트렌드' 발표자료입니다. file hooni 2014.10.02 976
764 Develop [ios] Xcode의 디버그 모드에서 콜스택 file hooni 2015.01.03 997
763 Develop [js] 스크롤 이벤트 막기 hooni 2015.04.14 1007
762 Develop [c] FSN 온라인 코딩 테스트 (Sorting, Binary Search) file hooni 2015.06.26 1011
761 Develop [ios] Did UIScrollView End Scrolling? hooni 2016.04.19 1015
760 Develop [ios] 로컬에 있는 html 실행하기 hooni 2015.02.10 1026
759 Develop [ios] 최상위 ViewController 가져오기 hooni 2015.10.12 1057
758 Develop [ios][swift] 초간단 To-do Memo file hooni 2016.06.27 1061
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 71 Next
/ 71