💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
| 字符编码 | | | --- | --- | | mb_detect_encoding(str) | 检测字符串使用的是何种字符编码,返回UTF-8,ASCII等 | | 字符串编码转换 | | | --- | --- | ``` /** * [ugcode_vod 字符串编码转换] * @param integer $type [0为UTF-8转GB2312,1为GB2312转UTF-8] * @param string $str [要转换的字符串] * @return [type] [返回转换之后的字符串] */ function ugcode_vod($str='', $type=0) { if($type=='1'){ $str = mb_convert_encoding($data,"GB2312","UTF-8"); $result= iconv("GB2312","GB2312",$str); }else{ $str = mb_convert_encoding($data,"UTF-8","GB2312"); $result= iconv("UTF-8","UTF-8",$str); } return $result; } ``` | unicode字符转换 | | | --- | --- | ``` /** * [unicode2Char 将unicode字符转换成字符] * @param [type] $unicode [需要转换的unicode字符] * @return [type] [description] */ function unicode2Char($unicode){ if($unicode < 128) return chr($unicode); if($unicode < 2048) return chr(($unicode >> 6) + 192) . chr(($unicode & 63) + 128); if($unicode < 65536) return chr(($unicode >> 12) + 224) . chr((($unicode >> 6) & 63) + 128) . chr(($unicode & 63) + 128); if($unicode < 2097152) return chr(($unicode >> 18) + 240) . chr((($unicode >> 12) & 63) + 128) . chr((($unicode >> 6) & 63) + 128) . chr(($unicode & 63) + 128); return false; } /** * [char2Unicode 将字符转换成unicode字符] * @param [type] $char [需要转成unicode字符的字符串] * @return [type] [description] */ function char2Unicode($char){ switch (strlen($char)){ case 1 : return ord($char); case 2 : return (ord($char{1}) & 63) | ((ord($char{0}) & 31) << 6); case 3 : return (ord($char{2}) & 63) | ((ord($char{1}) & 63) << 6) | ((ord($char{0}) & 15) << 12); case 4 : return (ord($char{3}) & 63) | ((ord($char{2}) & 63) << 6) | ((ord($char{1}) & 63) << 12) | ((ord($char{0}) & 7) << 18); default : trigger_error('Character is not UTF-8!', E_USER_WARNING); return false; } } ```