日期:2014-05-17 浏览次数:20482 次
/** * 得到utf8 的长度 * @author mu_rain * @param String $str * @return Int */ static function strlen_utf8($str) { $i = 0; $count = 0; $len = strlen ($str); while ($i < $len) { $chr = ord ($str[$i]); $count++; $i++; if($i >= $len) break; if($chr & 0x80) { $chr <<= 1; while ($chr & 0x80) { $i++; $chr <<= 1; } } } return $count; }
------解决方案--------------------
function myLen($str,$startTag='[',$endTag=']',$encoding='utf-8') { $st = preg_quote($startTag); $et = preg_quote($endTag); return mb_strlen(preg_replace("#{$st}[^{$et}]*{$et}#","~",$str),$encoding); } echo myLen("123asd你好[哈哈]");//9
------解决方案--------------------
附赠字符串函数一堆 ,慢用。
<?php /** * Created by JetBrains PhpStorm. * User: Administrator * Date: 12-2-17 * Time: 上午11:43 * To change this template use File | Settings | File Templates. */ /** * 字符串处理基类. * @author mu_rain */ class kString{ // ------------------------------------ /** * replace the base64_encode to encode the string * * @param $str 对象的实例 * @package KDG * @subpackage String * @category Putils * @author jim * @return mixed */ // ------------------------------------ public static function encode($str){ $src = array("/","+","="); $dist = array("-a","-b","-c"); $old = base64_encode($str); $new = str_replace($src,$dist,$old); return $new; } // ------------------------------------ /** * replace the base64_decode to decode the string * * @param $str 对象的实例 * @package P * @subpackage String * @category Putils * @author jim * @return mixed */ // ------------------------------------ public static function decode($str){ $src = array("-a","-b","-c"); $dist = array("/","+","="); $old = str_replace($src,$dist,$str); $new = base64_decode($old); return $new; } // ------------------------------------ /** * replace the base64_decode to decode the string * * @param $str 对象的实例 * @package P * @subpackage String * @category Putils * @author jim * @return mixed */ // ------------------------------------ public static function showAsFileSize($str){ // change 1024 to 1k $count = intVal($str); $destSize = $count/1024; $destSize = round($destSize,1); return $destSize."K"; } }