Created
May 4, 2012 14:59
-
-
Save xixilive/2595310 to your computer and use it in GitHub Desktop.
php function, Base32 string encoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* StringUtil | |
* | |
* @author mickey | |
* @version 1.0 | |
* @package Utility | |
*/ | |
final class StringUtil | |
{ | |
/** | |
* 字符串编码表,32个无重复字符 | |
* 顺序有关,不同顺序会编码出不同结果 | |
* @var stirng | |
*/ | |
private static $_STR_ENC_CODE_TABLE="123456abcdefghijklmnopqrstuvwxyz"; | |
private static $_STR_ENC_BIT5 = 5; | |
private static $_STR_ENC_BIT8 = 8; | |
/** | |
* 编码一个字符串 | |
* | |
* @param string $keywords | |
* @return string | |
*/ | |
public static function encodeString($string) | |
{ | |
if(empty($string)) | |
{ | |
return ""; | |
} | |
$string=strtolower($string); | |
$encode_str=''; | |
$bit_str=''; | |
//串联字符串的二进制表示 | |
for($i=0;$i<strlen($string);$i++) | |
{ | |
$bit_str.=sprintf('%08b',ord($string[$i])); | |
} | |
//按5Bit分组 | |
$nbit5=strlen($bit_str)%self::$_STR_ENC_BIT5==0?strlen($bit_str)/self::$_STR_ENC_BIT5:intval(strlen($bit_str)/self::$_STR_ENC_BIT5)+1; | |
$bit5_arr=array(); | |
$i=0; | |
while($i<$nbit5) | |
{ | |
$bit5_arr[]= substr($bit_str,$i*self::$_STR_ENC_BIT5,self::$_STR_ENC_BIT5); | |
$i++; | |
} | |
//对Bit5数组补位:左边000,不足8位再在后面补0,对齐到8位 | |
$bit5_arr_pad=array(); | |
foreach($bit5_arr as $bit) | |
{ | |
$bit='000'.$bit; | |
$bit=str_pad($bit,8,'0'); | |
$bit5_arr_pad[]=$bit; | |
} | |
//从词典索引查找字符,组合成最终的编码串 | |
foreach($bit5_arr_pad as $v) | |
{ | |
$encode_str.=substr(self::$_STR_ENC_CODE_TABLE,bindec($v),1); | |
} | |
return $encode_str; | |
} | |
/** | |
* 解码一个由StringUtil::encode方法编码的字符串 | |
* | |
* @param string $string 待解码的字符串 | |
* @return string | |
*/ | |
public static function decodeString($encodedString) | |
{ | |
if(empty($encodedString)) | |
{ | |
return ""; | |
} | |
$string=strtolower($encodedString); | |
$decode_str=''; | |
$bit_str=''; | |
//合并待解码字符串在词典的索引值的二进制表示 | |
for($i=0;$i<strlen($string);$i++) | |
{ | |
$bit_str.=sprintf('%08b',strpos(self::$_STR_ENC_CODE_TABLE,$string[$i])); | |
} | |
//按8位1组进行分解 | |
$bit8_arr=array(); | |
$i=0; | |
while($i<strlen($bit_str)/self::$_STR_ENC_BIT8) | |
{ | |
$bit8_arr[]= substr($bit_str,$i*self::$_STR_ENC_BIT8,self::$_STR_ENC_BIT8); | |
$i++; | |
} | |
//清理左边的000,并重新合并 | |
$bit8_arr_fixed=array(); | |
foreach($bit8_arr as $bit) | |
{ | |
$bit=substr($bit,3); | |
$bit8_arr_fixed[]=$bit; | |
} | |
$bit_str=implode('',$bit8_arr_fixed); | |
//再按8位1组进行分解,得到二进制值 | |
$nbit8=intval(strlen($bit_str)/self::$_STR_ENC_BIT8); | |
$bit8_arr=array(); | |
$i=0; | |
while($i<$nbit8) | |
{ | |
$bit8_arr[]= substr($bit_str,$i*self::$_STR_ENC_BIT8,self::$_STR_ENC_BIT8); | |
$i++; | |
} | |
reset($bit8_arr); | |
//转化字符串 | |
foreach($bit8_arr as $v) | |
{ | |
$decode_str.=chr(bindec($v)); | |
} | |
return $decode_str; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment