- Header (' Content-type:text/html;charset=utf-8 ');
- /**
- * Statistics Chinese string length function
- * @param $str string to calculate the length
- * @param $type calculated length type, 0 (default) means one character in Chinese, 1 for a Chinese figure of two characters
- * @http://bbs.it-home.org
- *
- */
- function Abslength ($STR)
- {
- if (empty ($STR)) {
- return 0;
- }
- if (function_exists (' Mb_strlen ')) {
- Return Mb_strlen ($str, ' utf-8 ');
- }
- else {
- Preg_match_all ("/./u", $str, $ar);
- return count ($ar [0]);
- }
- }
- $STR = ' Scripting Academy welcome everyone, Ye!. ';
- $len = Abslength ($STR);
- Var_dump ($len); Return 12
- $len = Abslength ($str, ' 1 ');
- Echo '
'. $len; return 22
- /*
- Utf-8 code to intercept Chinese strings, parameters can refer to the SUBSTR function
- @param $str the string to intercept
- @param $start start position to intercept, negative number is reverse intercept
- @param $end the length to be intercepted
- */
- function Utf8_substr ($str, $start =0) {
- if (empty ($STR)) {
- return false;
- }
- if (function_exists (' mb_substr ')) {
- if (Func_num_args () >= 3) {
- $end = Func_get_arg (2);
- Return Mb_substr ($str, $start, $end, ' utf-8 ');
- }
- else {
- Mb_internal_encoding ("UTF-8");
- Return Mb_substr ($str, $start);
- }
- }
- else {
- $null = "";
- Preg_match_all ("/./u", $str, $ar);
- if (Func_num_args () >= 3) {
- $end = Func_get_arg (2);
- Return join ($null, array_slice ($ar [0], $start, $end));
- }
- else {
- Return join ($null, array_slice ($ar [0], $start));
- }
- }
- }
- $str 2 = ' wo to intercept Zhongwen ';
- Echo '
';
- Echo utf8_substr ($str 2,0,-4); Return wo to intercept Zhon
- ?>
Copy Code2, support Gb2312,gbk,utf-8,big5 Chinese interception method
- /*
- * Chinese interception, support Gb2312,gbk,utf-8,big5
- * bbs.it-home.org
- * @param string $str the string to intercept
- * @param int $start intercept start position
- * @param int $length intercept length
- * @param string $charset UTF-8|GB2312|GBK|BIG5 encoding
- * @param $suffix whether to add suffix
- */
- Public Function Csubstr ($STR, $start =0, $length, $charset = "Utf-8", $suffix =true)
- {
- if (function_exists ("Mb_substr"))
- {
- if (Mb_strlen ($str, $charset) <= $length) return $str;
- $slice = Mb_substr ($str, $start, $length, $charset);
- }
- Else
- {
- $re [' utf-8 '] = "/[\x01-\x7f]| [\XC2-\XDF] [\x80-\xbf]| [\xe0-\xef] [\X80-\XBF] {2}| [\xf0-\xff] [\X80-\XBF] {3}/";
- $re [' gb2312 '] = "/[\x01-\x7f]| [\xb0-\xf7] [\xa0-\xfe]/];
- $re [' gbk '] = "/[\x01-\x7f]| [\x81-\xfe] [\x40-\xfe]/];
- $re [' big5 '] = "/[\x01-\x7f]| [\x81-\xfe] ([\x40-\x7e]|\xa1-\xfe]) /";
- Preg_match_all ($re [$charset], $STR, $match);
- if (count ($match [0]) <= $length) return $str;
- $slice = Join ("", Array_slice ($match [0], $start, $length));
- }
- if ($suffix) return $slice. " ...";
- return $slice;
- }
- ?>
Copy Code |