|
|
string pack ( string format [, mixed args [, mixed ...]] )
format是第一个变量,表示pack的格式。
| Code | Description | | a | \x00 填充 | | A | 空格 填充 | | h | 16进制数据,低位在前 | | H | 16进制数据,高位之前 | | c | 1字节数据,取值(有符号):-128~127 | | C | 1字节数据,取值(无符号):0~255 「C(255)=c(127)」 | | s | 有符号16位数字,根据机器设置的位顺序 | | S | 无符号16位整型,根据机器设置的位顺序 | | n | 无符号16位整型,高位在前 | | v | 无符号16位整型,低位在前 | | i | 有符号整型,根据机器设置的大小和顺序 | | I | 无符号整型,根据机器设置的大小和顺序 | | l | 有符号32位整型,高位在前 | | L | 无符号32位整型,根据机器设置的位顺序 | | N | 无符号32位整型,高位在前 | | V | 无符号32位整型,低位在前 | | f | 浮点,根据机器设置的大小和顺序 | | d | 双精度浮点,根据机器设置的大小和顺序 | | x | 空值(不知道具体特性) | | X | 倒回一位(不知道具体特性) | | @ | 在剩余位置 用NULL 字符填充(不知道具体特性) |
从实用出发,我们介绍几个实用的例子,
- <?php
- $a=pack('N4', 123456789, 987654321, 1234444, 6548793);
- print_r(unpack('N4', $a));
- list(, $_1, $_2, $_3, $_4)=unpack('N4', $a);
- echo '<br/>';
- echo "1={$_1}<br/>";
- echo "2={$_2}<br/>";
- echo "3={$_3}<br/>";
- echo "4={$_4}";
- ?>
复制代码 运行结果,输出如下
Array ( [1] => 123456789 [2] => 987654321 [3] => 1234444 [4] => 6548793 )
1=123456789
2=987654321
3=1234444
4=6548793 存储一段数据,这段代码来自QQ机器人,非常有实用价值
- <?php
- //载入$StorageData为积分
- //$StorageData['Talks'] 聊天数
- //$StorageData['Money'] 金钱
- //$StorageData['E1-6'] 六种未用积分
- $StorageData=array(10000000,2,3,4,5,6,7,8); #你可以自行修改测试 = = 注意各种数据的存储范围
-
- $pack=call_user_func_array('pack',array_merge(array('I1s7'),$StorageData));
-
- print_r(unpack('I1Talks/s1Money/s1E1/s1E2/s1E3/s1E4/s1E5/s1E6', $pack));
- ?>
复制代码
运行结果,输出如下
Array ( [Talks] => 10000000 [Money] => 2 [E1] => 3 [E2] => 4 [E3] => 5 [E4] => 6 [E5] => 7 [E6] => 8 )
如果你要创建文本数据库,并且每一行的数据都是固定大小的,你可以借助pack将数据打包~用unpack打开,这个还是不错的
注意读写二进制文件要加b模式~
填数据,可以快速获得pack、unpack的字串
- <?php
- define('R_STRING',1);
- define('R_SHORT',2);
- define('R_INT',3);
- define('R_LONG', 4);
- define('R_TINY',5);
- define('R_HEX',6);
- define('R_SIGNED',1);
- define('R_UNSIGNED',0);
-
- $Table=array(
- 'uid'=>array(R_INT, R_UNSIGNED),
- 'username'=>array(R_STRING, 13), //用户名,最多13个字节
- 'password'=>array(R_HEX),
- 'adminid'=>array(R_INT, R_UNSIGNED),
- 'groupid'=>array(R_SHORT, R_UNSIGNED),
- 'lastpost'=>array(R_INT, R_UNSIGNED),
- 'credits'=>array(R_INT, R_UNSIGNED),
- 'extcredits1'=>array(R_LONG, R_UNSIGNED),
- 'extcredits2'=>array(R_INT, R_UNSIGNED),
- 'extcredits3'=>array(R_INT, R_UNSIGNED),
- 'extcredits4'=>array(R_INT, R_UNSIGNED),
- 'extcredits5'=>array(R_INT, R_UNSIGNED),
- 'extcredits6'=>array(R_INT, R_UNSIGNED),
- 'extcredits7'=>array(R_INT, R_UNSIGNED),
- 'extcredits8'=>array(R_INT, R_UNSIGNED),
- 'email'=>array(R_STRING, 40)
- //列名不能以数字开头
- );
-
- function addSpecialToPack($ROW) {
- if($ROW==''){
- $GLOBALS['PACK'].=$GLOBALS['LastPack'].($GLOBALS['LastPackTimes']==1?''$GLOBALS['LastPackTimes']);
- $GLOBALS['LastPack']='';
- $GLOBALS['LastPackTimes']=0;
- return true;
- }
- if($GLOBALS['LastPack']==$ROW){
- $GLOBALS['LastPackTimes']++;
- }else{
- $GLOBALS['PACK'].=$GLOBALS['LastPack'].($GLOBALS['LastPackTimes']==1?''$GLOBALS['LastPackTimes']);
- $GLOBALS['LastPack']=$ROW;
- $GLOBALS['LastPackTimes']=1;
- }
- return true;
- }
-
- $PACK='';
- $LastPack='';
- $LastPackTimes='';
- $UNPACK=array();
- $TINY=array();
- foreach($Table as $Name => $Row){
- switch($Row[0]){
- case R_STRING:
- addSpecialToPack();
- $PACK.='a'.$Row[1];
- $UNPACK[]='a'.$Row[1].$Name;
- break;
- case R_HEX:
- addSpecialToPack('H');
- $UNPACK[]='H'.$Name;
- break;
- case R_SHORT:
- $MOD=($Row[1]==R_SIGNED)?'s':'n';
- addSpecialToPack($MOD);
- $UNPACK[]=$MOD.$Name;
- break;
- case R_INT:
- $MOD=($Row[1]==R_SIGNED)?'i':'I';
- addSpecialToPack($MOD);
- $UNPACK[]=$MOD.$Name;
- break;
- case R_LONG:
- $MOD=($Row[1]==R_SIGNED)?'l':'N';
- addSpecialToPack($MOD);
- $UNPACK[]=$MOD.$Name;
- break;
- default;
- break;
- }
- }
-
- print_r(array($PACK, implode('/', $UNPACK)));
-
- //三个组合,代入指定程序,几乎可实现无索引的表
- ?>
复制代码Array ( [0] => Ia130HInI2NI7a40 [1] => Iuid/a13username/Hpassword/Iadminid/ngroupid/Ilastpost/Icredits/Nextcredits1/Iextcredits2/Iextcredits3/Iextcredits4/Iextcredits5/Iextcredits6/Iextcredits7/Iextcredits8/a40email )
|
|
|