Home  >  Article  >  Backend Development  >  100分求个 检拆字符串长度

100分求个 检拆字符串长度

WBOY
WBOYOriginal
2016-06-13 13:21:32722browse

100分求个 检测字符串长度
$str[0] = "123asd你好[哈哈]"
$str[1] = "123asd你好[你好不好啊]"
//一个中文一个字符(utf-8的编码) 符号 [这里面的字和外边的符号算一个字符] 
然后计算长度
结果是$str[0]和$str[1]为长度9

最好写成一个函数。谢谢了


------解决方案--------------------
我没太看懂楼主的题,不过函数,手头上有一个。

PHP code

/**
     * 得到utf8 的长度
     * @author mu_rain
     * @param String $str
     * @return Int
     */
    static function strlen_utf8($str) {
        $i = 0;
        $count = 0;
        $len = strlen ($str);
        while ($i = $len) break;
            if($chr & 0x80) {
                $chr <font color="#e78608">------解决方案--------------------</font><br>
PHP code
    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
<br><font color="#e78608">------解决方案--------------------</font><br>附赠字符串函数一堆 ,慢用。<br>
PHP code
<?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";
    }


  

} <div class="clear">
                 
              
              
        
            
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn