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='') { if (empty($str)) { return NULL; } if(empty($enclist)){ $enclist=array('utf-8','gb2312','big5','gbk'); } foreach ((array)$enclist as $v) { //$v = strtolower($v); if ($v == 'utf-8') { $v = 'utf8'; } if ($v($str)) { if ($v == 'utf8') { return 'utf-8'; } return $v; } } if (function_exists('mb_detect_encoding')) { if (!empty($enclist)) { $return = mb_detect_encoding($str, $enclist); if (!empty($return)) return strtolower($return); } } return NULL; } /** * 字符串截取,支持中文和其他编码 */ function msubstr($str, $start=0, $length, $enclist='') { if(empty($str))return NULL; $charset=bianma($str,$enclist); if(function_exists("mb_substr")) $slice = mb_substr($str, $start, $length,$charset); elseif(function_exists('iconv_substr')) { $slice = iconv_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); $slice = join("",array_slice($match[0], $start, $length)); } return $slice; } //混合字符串长度 function mstrlen($str,$enclist='') {if(empty($str))return 0; $charset=bianma($str,$enclist); if(function_exists("mb_strlen")) $length = mb_strlen($str,$charset); elseif(function_exists('iconv_strlen')) { $length = iconv_strlen($str,$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); $length =count($match[0]); } return $length; } // 自动转换字符集 function charset($string,$to='utf-8',$from='',$enclist='') { if (empty($string)||empty($to)) { return $string;} if(empty($from)){ $from=bianma($string,$enclist);} if(empty($from)||($from==$to)){return $string;} if (function_exists('mb_convert_encoding')) { return mb_convert_encoding($string, $to, $from); } elseif (function_exists('iconv')) { return iconv($from, $to, $string); } else { return $string; } } /** * 产生随机字串,可用来自动生成密码 */ function randstring($len=6,$u=false,$charlist='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') { $str ='';$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='') { if(empty($index)) $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $ret = ''; 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='') { if(empty($index)) $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $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 = 'ENCODE', $key = '', $expiry = 0) { $ckey_length = 4; $key = md5($key != '' ? $key : pc_base::load_config('system', 'auth_key')); $keya = md5(substr($key, 0, 16)); $keyb = md5(substr($key, 16, 16)); $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya.md5($keya.$keyc); $key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(strtr(substr($string, $ckey_length), '-_', '+/')) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string; $string_length = strlen($string); $result = ''; $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 == 'DECODE') { 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 ''; } } else { return $keyc.rtrim(strtr(base64_encode($result), '+/', '-_'), '='); } } /** * gbk转拼音 * @param $txt */ function gbk_to_pinyin($txt) { $txt=charset($txt,'gbk'); $l = strlen($txt); $i = 0; $pyarr = array(); $py = array(); $filename = __DIR__.'/gb-pinyin.table'; $fp = fopen($filename,'r'); 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,'big5'); $BIG5_DATA = ''; $filename = __DIR__.'/big5-gb.table'; $fp = fopen($filename, 'rb'); $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 = ' '; } 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,'gbk'); $GB_DATA=''; $filename = __DIR__.'/gb-big5.table'; $fp = fopen($filename, 'rb'); $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 = ' '; } 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; }
2.string.php
<?php require_once('../lib/string/fun.php'); //utf8检查 //参数 待检字符串,是否排除单字节 var_dump(utf8('的订单13052561263',1)); //gbk gb2312 big5检查 var_dump(gbk('的订单13052561263')); //检查编码 //参数 待检字符串 编码次序 var_dump(bianma('的订单13052561263')); //字符串截取,支持中文和其他编码 //参数 字符串,起始位置,长度,编码次序 echo msubstr('fg随g时bn随b地t',0,6),'<br/>'; //混合字符串长度 //参数 字符串,编码次序 echo mstrlen('fg随g时bn随b地t'),'<br/>'; //自动转换字符集 //参数 字符串,目标编码,来源编码,编码次序 echo charset('寶龍','big5'),'<br/>'; //产生随机字串 //参数 长度,是否支持中文字符,字符集 echo randstring('5',true,'宝个龙2wet'),'<br/>'; //10进制转为n进制 //参数 10进制数 ,目标进制,目标字符集 echo decn( '4340277777777777664',64,'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@'),'<br/>'; //n进制转为10进制 //参数 n进制数 ,目标进制,目标字符集 echo dec10('ab33',16),'<br/>'; //字符串加密、解密函数 //参数 字符串,ENCODE为加密,DECODE为解密,密钥,过期时间 $s=sys_auth('ss','ENCODE','ybl'); echo sys_auth($s,'DECODE','ybl'),'<br/>'; //gbk转拼音 var_dump(gbk_to_pinyin('订单到底')); echo '<br/>'; //繁体转简体 echo charset(big5_to_gbk('寶龍'),'gbk'),'<br/>'; //简体转繁体 echo gbk_to_big5('宝龙'),'<br/>';