| string pack ( string format [, mixed args [, mixed ...]] )
format是第一个变量,表示pack的格式。
从实用出发,我们介绍几个实用的例子, <?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 )存储一段数据,这段代码来自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 ) |
2015-03-05 10:14:08
4080
2015-03-05 10:14:08