PHP指定字符串长度截取

在取数据的时候可以控制长度,加密算法也可以用到。

function sysSubStr($pass,$Length)
{
    if (strlen($pass) <= $Length )
    {
        return $pass;
    }
    else
    {
        $I = 0;
        while ($I < $Length)
        {
            $StringTMP = substr($pass,$I,1);
            if ( ord($StringTMP) >=224 )
            {
                $StringTMP = substr($pass,$I,3);
                $I = $I + 3;
            }
            elseif( ord($StringTMP) >=192 )
            {
                $StringTMP = substr($pass,$I,2);
                $I = $I + 2;
            }
            else
            {
                $I = $I + 1;
            }
            $StringLast[] = $StringTMP;
        }
        $StringLast = implode("",$StringLast);
        return $StringLast;
    }
}

 

 

posted @ 2012-08-05 14:27  高级园长  阅读(276)  评论(0编辑  收藏  举报