php字符串截取,检测字符串编码方法
2012-06-28 16:30
519 查看
<?php /** * 中文字符串截取 */ function subString($str, $start, $length) { $i = 0; //完整排除之前的UTF8字符 while($i < $start) { $ord = ord($str{$i}); if($ord < 192) { $i++; } elseif($ord < 224) { $i += 2; } else { $i += 3; } } //开始截取 $result = ''; while($i < $start + $length && $i < strlen($str)) { $ord = ord($str{$i}); if($ord < 192) { $result .= $str{$i}; $i++; } elseif($ord <224) { $result .= $str{$i}.$str{$i+1}; $i += 2; } else { $result .= $str{$i}.$str{$i+1}.$str{$i+2}; $i += 3; } } if($i < strlen($str)) { $result .= '...'; } return $result; } /** * 检测编码 */ function safeEncoding($string,$outEncoding = 'UTF-8') { $encoding = "UTF-8"; for($i = 0; $i<strlen($string); $i++) { if(ord($string{$i}) < 128) { continue; } if((ord($string{$i}) & 224) == 224) { //第一个字节判断通过 $char = $string{++$i}; if((ord($char) & 128) == 128) { //第二个字节判断通过 $char = $string{++$i}; if((ord($char) & 128) == 128) { $encoding = "UTF-8"; break; } } } if((ord($string{$i}) & 192) == 192) { //第一个字节判断通过 $char = $string{++$i}; if((ord($char) & 128) == 128) { //第二个字节判断通过 $encoding = "GB2312"; break; } } } if(strtoupper($encoding) == strtoupper($outEncoding)) { return $string; } else { return iconv($encoding,$outEncoding,$string); } } class splite_utf8 { private function splite_single_utf8_left_word ($str ) { $aciss = ord( $str); $out_str = ''; if ($aciss >= 240 ) { $out_str.=substr ( $str, 0, 4 ); } elseif ($aciss >= 224 ) { $out_str.=substr ( $str, 0, 3 ); } elseif ($aciss >= 192 ) { $out_str.=substr ( $str, 0, 2 ); } else { $out_str.=substr ($str, 0, 1 ); } return $out_str; } private function splite_single_utf8_right_word ($str ) { $aciss = ord( $str); $out_str = ''; if ($aciss >= 240 ) { $out_str.=substr ( $str, 4 ); } elseif ($aciss >= 224 ) { $out_str.= substr ( $str, 3 ); } elseif ($aciss >= 192 ) { $out_str.= substr ( $str, 2 ); } else { $out_str.= substr ($str, 1 ); } return $out_str; } public function count_word($str, $length=0 ) { $aciss = ord( $str); if ($aciss >= 240 ) { $length+= 1; $str=substr($str,4); } elseif ($aciss >= 224 ) { $length+= 1; $str=substr($str,3); } elseif ($aciss >= 192 ) { $length+= 1; $str=substr($str,2); } else { $length+= 1; $str=substr($str,1); } if($str=='') { return $length; } else { return $this->count_word($str,$length); } } public function splite_mulit_utf8_word ($str, $start = 0, $length = -1 ) { $temp = ''; if($start < 0 ) { $start = $this->count_word($str) + $start; } for ($i = 0; $i < $start; $i++ ) { $str=$this->splite_single_utf8_right_word ($str ); } for ($i = 0; $i < $length; $i++ ) { $temp.= $this->splite_single_utf8_left_word ($str ); $str = $this->splite_single_utf8_right_word ($str ); } if( $length == -1 ) { return $str; } else { return $temp; } } } $utf=new splite_utf8(); $text='的萨芬dfdf!@#$%^&*I()'; $length=$utf->count_word($text); echo $length."/n"; $word=$utf->splite_mulit_utf8_word ($text, 3, 2); var_dump($word); ?>
相关文章推荐
- PHP检测字符串是否为UTF8编码的常用方法
- php检测字符串编码(utf-8,gbk,gb2312)是否为utf8编码的方法总结
- php检测文件/字符串编码方法
- php检测字符串编码(utf-8,gbk,gb2312)是否为utf8编码的方法总结
- php中按字节截取字符串方法,(汉字占两个字节,字母占一个字节,页面编码必须为utf-8)
- PHP检测字符串是否为UTF8编码的常用方法
- PHP检测字符串是否为UTF8编码的常用方法
- PHP检测字符串是否为UTF8编码4种方法
- php检测文件编码的方法示例
- PHP截取中文字符串方法合集
- PHP截取中文字符串方法
- php截取字符串方法, 让英文,中文,中英文混合差不多长
- PHP截取中文字符串方法总结
- php中文字符串截取方法实例总结
- PHP截取中文字符串方法总结
- php 截取字符串第一个字符,截取掉字符串最后一个字符的方法
- Php检测文件编码方法
- PHP----字符串(支持中文和其它编码)截取,后加自定义标识符
- PHP截取中文字符串方法总结
- PHP针对中英文混合字符串长度判断及截取方法示例