Heim > php教程 > PHP源码 > Hauptteil

混合字符串函数

PHP中文网
Freigeben: 2016-05-23 08:39:37
Original
1170 Leute haben es durchsucht

1.fun.php

<?php 
function utf8($str,$u=false)
{
    if (substr($str, 0, 3) == chr(239) . chr(187) . chr(191)) {
        $str = substr($str, 3);
    } //去掉bom头
    if (empty($str))
        return false;
    $c   = $c2 = $c3 = 0;
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        $c = ord($str[$i]);
        if ($c < 128){
            if($u) return false; else continue;
        }
             
        elseif ($c > 127 && $c < 194)
            return false;
        elseif ($c > 193 && $c < 224) {
            $i++;
            $c = ord($str[$i]);
            if ($c > 127 && $c < 192) {
                continue;
            } else {
                return false;
            }
        } elseif ($c > 223 && $c < 240) {
            $i++;
            $c = ord($str[$i]);
            $i++;
            $c2 = ord($str[$i]);
            if (($c > 127 && $c < 192) && ($c2 > 127 && $c2 < 192)) {
                continue;
            } else {
                return false;
            }
        } elseif ($c > 239 && $c < 256) {
            $i++;
            $c = ord($str[$i]);
            $i++;
            $c2 = ord($str[$i]);
            $i++;
            $c3 = ord($str[$i]);
            if (($c > 127 && $c < 192) && ($c2 > 127 && $c2 < 192) && ($c3 > 127 && $c3 < 192)) {
                continue;
            } else {
                return false;
            }
        } elseif ($c > 255)
            return false;
    }
    return true;
}
 
function gbk($str,$u=false)
{
    if (empty($str))
        return false;
    $c   = 0;
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        $c = ord($str[$i]);
        if ($c < 128){
            if($u) return false; else continue;}
        elseif ($c == 128)
            return false;
        elseif ($c > 128 && $c < 255) {
            $i++;
            $c = ord($str[$i]);
            if ($c > 63 && $c < 255) {
                continue;
            } else {
                return false;
            }
        } elseif ($c > 254)
            return false;
    }
    return true;
}
 
function gb2312($str,$u=false)
{
    if (empty($str))
        return false;
    $c   = 0;
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        $c = ord($str[$i]);
        if ($c < 128){
            if($u) return false; else continue;}
        elseif ($c > 127 && $c < 176)
            return false;
        elseif ($c > 175 && $c < 248) {
            $i++;
            $c = ord($str[$i]);
            if ($c > 159 && $c < 255) {
                continue;
            } else {
                return false;
            }
        } elseif ($c > 247)
            return false;
    }
    return true;
}
 
 
function big5($str,$u=false)
{
    if (empty($str))
        return false;
    $c   = 0;
    $len = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        $c = ord($str[$i]);
        if ($c < 128){
            if($u) return false; else continue;}
        elseif ($c == 128)
            return false;
        elseif ($c > 128 && $c < 255) {
            $i++;
            $c = ord($str[$i]);
            if (($c > 63 && $c < 127) || ($c > 160 && $c < 255)) {
                continue;
            } else {
                return false;
            }
        } elseif ($c > 254)
            return false;
    }
    return true;
}
 
 
function bianma($str,$enclist=&#39;&#39;)
{
    if (empty($str)) {
        return NULL;
    }
   if(empty($enclist)){
     $enclist=array(&#39;utf-8&#39;,&#39;gb2312&#39;,&#39;big5&#39;,&#39;gbk&#39;);  
   }
    foreach ((array)$enclist as $v) {
        //$v = strtolower($v);
        if ($v == &#39;utf-8&#39;) {
            $v = &#39;utf8&#39;;
        }
         if ($v($str)) {
            if ($v == &#39;utf8&#39;) {
                return &#39;utf-8&#39;;
            }
            return $v;
        }
    }
    if (function_exists(&#39;mb_detect_encoding&#39;)) {
        if (!empty($enclist)) {
            $return = mb_detect_encoding($str, $enclist);
            if (!empty($return))
                return strtolower($return);
        }
    }
    return NULL;
}
 
 
/**
     * 字符串截取,支持中文和其他编码
     */
     function msubstr($str, $start=0, $length, $enclist=&#39;&#39;) {
         if(empty($str))return NULL;
         $charset=bianma($str,$enclist);
        if(function_exists("mb_substr"))
            $slice = mb_substr($str, $start, $length,$charset);
        elseif(function_exists(&#39;iconv_substr&#39;)) {
            $slice = iconv_substr($str,$start,$length,$charset);
        }else{
            $re[&#39;utf-8&#39;]   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re[&#39;gb2312&#39;] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re[&#39;gbk&#39;]    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re[&#39;big5&#39;]   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|[\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            $slice = join("",array_slice($match[0], $start, $length));
        }
        return $slice;
    }
    //混合字符串长度
 function mstrlen($str,$enclist=&#39;&#39;) {if(empty($str))return 0;
       $charset=bianma($str,$enclist);
        if(function_exists("mb_strlen"))
            $length = mb_strlen($str,$charset);
        elseif(function_exists(&#39;iconv_strlen&#39;)) {
            $length = iconv_strlen($str,$charset);
        }else{
        $re[&#39;utf-8&#39;]   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re[&#39;gb2312&#39;] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re[&#39;gbk&#39;]    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re[&#39;big5&#39;]   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|[\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            $length =count($match[0]);
        }
        return $length;
    }
// 自动转换字符集 
     function charset($string,$to=&#39;utf-8&#39;,$from=&#39;&#39;,$enclist=&#39;&#39;) {
    if (empty($string)||empty($to)) {
            return $string;}  
    if(empty($from)){
        $from=bianma($string,$enclist);}
    if(empty($from)||($from==$to)){return $string;}
            if (function_exists(&#39;mb_convert_encoding&#39;)) {
                return mb_convert_encoding($string, $to, $from);
            } elseif (function_exists(&#39;iconv&#39;)) {
                return iconv($from, $to, $string);
            } else {
                return $string;
            }
         
    }   
    /**
     * 产生随机字串,可用来自动生成密码
     */
     function randstring($len=6,$u=false,$charlist=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&#39;) {
        $str =&#39;&#39;;$repeat=1;
    if($u){$l=mstrlen($charlist);   
            for($i=0;$i<$len;$i++){  
             $str.=msubstr($charlist, floor(mt_rand(0,$l-1)),1);
            }   
                 
            }else{
                $repeat=ceil($len/strlen($charlist));
                $charlist=str_shuffle(str_repeat($charlist,$repeat));
             $str     =   substr($charlist,0,$len); 
            }
            
        
        return $str;
    }
//10进制转为n进制  
function decn($n,$base=62,$index=&#39;&#39;) {  
if(empty($index))
    $index = &#39;0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;;  
    $ret = &#39;&#39;;  
    for($t = floor(log10($n) / log10($base)); $t >= 0; $t --) {  
        $a = floor($n / pow($base, $t));  
        $ret .= substr($index, $a, 1);  
        $n -= $a * pow($base, $t);  
    }  
    return $ret;  
}   
     
    //n进制转为10进制  
function dec10($s,$base=62,$index=&#39;&#39;) {  
if(empty($index))
    $index = &#39;0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;;  
    $ret = 0;  
    $len = strlen($s) - 1;  
    for($t = 0; $t <= $len; $t ++) {  
        $ret += strpos($index, substr($s, $t, 1)) * pow($base, $len - $t);  
    }  
    return $ret;  
}
 
/**
* 字符串加密、解密函数
*
*
* @param    string  $txt        字符串
* @param    string  $operation  ENCODE为加密,DECODE为解密,可选参数,默认为ENCODE,
* @param    string  $key        密钥:数字、字母、下划线
* @param    string  $expiry     过期时间
* @return   string
*/
function sys_auth($string, $operation = &#39;ENCODE&#39;, $key = &#39;&#39;, $expiry = 0) {
    $ckey_length = 4;
    $key = md5($key != &#39;&#39; ? $key : pc_base::load_config(&#39;system&#39;, &#39;auth_key&#39;));
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == &#39;DECODE&#39; ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : &#39;&#39;;
 
    $cryptkey = $keya.md5($keya.$keyc);
    $key_length = strlen($cryptkey);
 
    $string = $operation == &#39;DECODE&#39; ? base64_decode(strtr(substr($string, $ckey_length), &#39;-_&#39;, &#39;+/&#39;)) : sprintf(&#39;%010d&#39;, $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    $string_length = strlen($string);
 
    $result = &#39;&#39;;
    $box = range(0, 255);
 
    $rndkey = array();
    for($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
 
    for($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
 
    for($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
 
    if($operation == &#39;DECODE&#39;) {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return &#39;&#39;;
        }
    } else {
        return $keyc.rtrim(strtr(base64_encode($result), &#39;+/&#39;, &#39;-_&#39;), &#39;=&#39;);
    }
}
/**
 * gbk转拼音
 * @param $txt
 */
function gbk_to_pinyin($txt) {
    $txt=charset($txt,&#39;gbk&#39;);
    $l = strlen($txt);
    $i = 0;
    $pyarr = array();
    $py = array();
    $filename = __DIR__.&#39;/gb-pinyin.table&#39;;
    $fp = fopen($filename,&#39;r&#39;);
    while(!feof($fp)) {
        $p = explode("-",fgets($fp,32));
        $pyarr[intval($p[1])] = trim($p[0]);
    }
    fclose($fp);
    ksort($pyarr);
    while($i<$l) {
        $tmp = ord($txt[$i]);
        if($tmp>=128) {
            $asc = abs($tmp*256+ord($txt[$i+1])-65536);
            $i = $i+1;
        } else $asc = $tmp;
        $py[] = asc_to_pinyin($asc,$pyarr);
        $i++;
    }
    return $py;
}
function asc_to_pinyin($asc,&$pyarr) {
    if($asc < 128)return chr($asc);
    elseif(isset($pyarr[$asc]))return $pyarr[$asc];
    else {
        foreach($pyarr as $id => $p) {
            if($id >= $asc)return $p;
        }
    }
}
/**
 * 繁体转简体
 * @param  $Text
 */
function big5_to_gbk($Text) {
    $Text=charset($Text,&#39;big5&#39;);
$BIG5_DATA = &#39;&#39;;
        $filename = __DIR__.&#39;/big5-gb.table&#39;;
        $fp = fopen($filename, &#39;rb&#39;);
        $BIG5_DATA = fread($fp, filesize($filename));
        fclose($fp);
 
    $max = strlen($Text)-1;
    for($i = 0; $i < $max; $i++) {
        $h = ord($Text[$i]);
        if($h >= 0x80) {
            $l = ord($Text[$i+1]);
            if($h==161 && $l==64) {
                $gbstr = &#39; &#39;;
            } else {
                $p = ($h-160)*510+($l-1)*2;
                $gbstr = $BIG5_DATA[$p].$BIG5_DATA[$p+1];
            }
            $Text[$i] = $gbstr[0];
            $Text[$i+1] = $gbstr[1];
            $i++;
        }
    }
    return $Text;
}
/**
 * 简体转繁体
 * @param  $Text
 */
function gbk_to_big5($Text) {   $Text=charset($Text,&#39;gbk&#39;);
$GB_DATA=&#39;&#39;;
        $filename =  __DIR__.&#39;/gb-big5.table&#39;;
        $fp = fopen($filename, &#39;rb&#39;);
        $GB_DATA = fread($fp, filesize($filename));
        fclose($fp);
    $max = strlen($Text)-1;
    for($i = 0; $i < $max; $i++) {
        $h = ord($Text[$i]);
        if($h >= 0x80) {
            $l = ord($Text[$i+1]);
            if($h==161 && $l==64) {
                $big = &#39; &#39;;
            } else {
                $p = ($h-160)*510+($l-1)*2;
                $big = $GB_DATA[$p].$GB_DATA[$p+1];
            }
            $Text[$i] = $big[0];
            $Text[$i+1] = $big[1];
            $i++;
        }
    }
    return $Text;
}
Nach dem Login kopieren

2.string.php

<?php
require_once(&#39;../lib/string/fun.php&#39;);
//utf8检查
//参数 待检字符串,是否排除单字节 
var_dump(utf8(&#39;的订单13052561263&#39;,1));
//gbk gb2312 big5检查
var_dump(gbk(&#39;的订单13052561263&#39;));
//检查编码
//参数 待检字符串 编码次序
var_dump(bianma(&#39;的订单13052561263&#39;));
//字符串截取,支持中文和其他编码
//参数 字符串,起始位置,长度,编码次序
echo msubstr(&#39;fg随g时bn随b地t&#39;,0,6),&#39;<br/>&#39;; 
//混合字符串长度
//参数 字符串,编码次序
echo mstrlen(&#39;fg随g时bn随b地t&#39;),&#39;<br/>&#39;; 
//自动转换字符集
//参数 字符串,目标编码,来源编码,编码次序
echo charset(&#39;寶龍&#39;,&#39;big5&#39;),&#39;<br/>&#39;; 
//产生随机字串
//参数 长度,是否支持中文字符,字符集
echo randstring(&#39;5&#39;,true,&#39;宝个龙2wet&#39;),&#39;<br/>&#39;; 
//10进制转为n进制  
//参数 10进制数 ,目标进制,目标字符集
echo decn( &#39;4340277777777777664&#39;,64,&#39;0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@&#39;),&#39;<br/>&#39;; 
//n进制转为10进制  
//参数 n进制数 ,目标进制,目标字符集
echo dec10(&#39;ab33&#39;,16),&#39;<br/>&#39;;
//字符串加密、解密函数
//参数 字符串,ENCODE为加密,DECODE为解密,密钥,过期时间
$s=sys_auth(&#39;ss&#39;,&#39;ENCODE&#39;,&#39;ybl&#39;);
echo sys_auth($s,&#39;DECODE&#39;,&#39;ybl&#39;),&#39;<br/>&#39;;
 
//gbk转拼音
var_dump(gbk_to_pinyin(&#39;订单到底&#39;)); 
echo &#39;<br/>&#39;;
//繁体转简体
echo charset(big5_to_gbk(&#39;寶龍&#39;),&#39;gbk&#39;),&#39;<br/>&#39;;
//简体转繁体
 echo gbk_to_big5(&#39;宝龙&#39;),&#39;<br/>&#39;;
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!