-
-
/** - @UTF-8 Chinese character truncation program
- @http://bbs.it-home.org
- */
- $str = "321 This is a test string";
- $str1 = "()()";
- echo subUTF8str ($str,0,3)."
";
- echo subUTF8str($str,0,4)."
";
- echo subUTF8str($str1,0,4)."< br>";
- echo subUTF8str($str1,0,10)."
";
function subUTF8str($str,$start=0,$length=80){
- $cur_len = 0; //The string length understood by humans
- $all_len = strlen($str); //The string length understood by machines
- if($length > $all_len)
- {
- return $str;
- }
- for($i = 0;$i < $all_len;)
- {
- if($cur_len == $start)
- {
- break;
- }
- if (ord($str[$i]) > 127 )
- {
- $i += 3;
- }else{
- $i += 1;
- }
- $cur_len ++;
- }
- $start_pos = $i;
- $temp_pos = $cur_len;
- for(;$ cur_len - $temp_pos < $length;)
- {
- if($i >= $all_len)
- break;
- if (ord($str[$i]) > 127)
- {
- $i += 3 ;
- }else{
- $i += 1;
- }
- $cur_len ++;
- }
- $end_pos = $i;
- return substr($str,$start_pos,$end_pos);
- }
- ?>
-
-
|