返回列表 发新帖
查看: 406|回复: 0

[Discuz!二次开发] PHP的pack函数详解

[复制链接]

6671

热度

1万

元宝

262

贡献

管理员

DZ专员

发表于 2015-3-5 10:14:08 | 显示全部楼层 |阅读模式
string pack ( string format [, mixed args [, mixed ...]] )
format是第一个变量,表示pack的格式。

CodeDescription
a\x00 填充
A空格 填充
h16进制数据,低位在前
H16进制数据,高位之前
c1字节数据,取值(有符号):-128~127
C1字节数据,取值(无符号):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 字符填充(不知道具体特性)

从实用出发,我们介绍几个实用的例子,
  1. <?php
  2. $a=pack('N4', 123456789, 987654321, 1234444, 6548793);
  3. print_r(unpack('N4', $a));
  4. list(, $_1, $_2, $_3, $_4)=unpack('N4', $a);
  5. echo '<br/>';
  6. echo "1={$_1}<br/>";
  7. echo "2={$_2}<br/>";
  8. echo "3={$_3}<br/>";
  9. echo "4={$_4}";
  10. ?>
复制代码
运行结果,输出如下
Array ( [1] => 123456789 [2] => 987654321 [3] => 1234444 [4] => 6548793 )
1=123456789
2=987654321
3=1234444
4=6548793
存储一段数据,这段代码来自QQ机器人,非常有实用价值
  1. <?php
  2. //载入$StorageData为积分
  3. //$StorageData['Talks'] 聊天数
  4. //$StorageData['Money'] 金钱
  5. //$StorageData['E1-6'] 六种未用积分
  6. $StorageData=array(10000000,2,3,4,5,6,7,8);  #你可以自行修改测试 = = 注意各种数据的存储范围

  7. $pack=call_user_func_array('pack',array_merge(array('I1s7'),$StorageData));

  8. print_r(unpack('I1Talks/s1Money/s1E1/s1E2/s1E3/s1E4/s1E5/s1E6', $pack));
  9. ?>
复制代码

运行结果,输出如下
Array ( [Talks] => 10000000 [Money] => 2 [E1] => 3 [E2] => 4 [E3] => 5 [E4] => 6 [E5] => 7 [E6] => 8 )

如果你要创建文本数据库,并且每一行的数据都是固定大小的,你可以借助pack将数据打包~用unpack打开,这个还是不错的

注意读写二进制文件要加b模式~

填数据,可以快速获得pack、unpack的字串
  1. <?php
  2. define('R_STRING',1);
  3. define('R_SHORT',2);
  4. define('R_INT',3);
  5. define('R_LONG', 4);
  6. define('R_TINY',5);
  7. define('R_HEX',6);
  8. define('R_SIGNED',1);
  9. define('R_UNSIGNED',0);

  10. $Table=array(
  11. 'uid'=>array(R_INT, R_UNSIGNED),
  12. 'username'=>array(R_STRING, 13),  //用户名,最多13个字节
  13. 'password'=>array(R_HEX),
  14. 'adminid'=>array(R_INT, R_UNSIGNED),
  15. 'groupid'=>array(R_SHORT, R_UNSIGNED),
  16. 'lastpost'=>array(R_INT, R_UNSIGNED),
  17. 'credits'=>array(R_INT, R_UNSIGNED),
  18. 'extcredits1'=>array(R_LONG, R_UNSIGNED),
  19. 'extcredits2'=>array(R_INT, R_UNSIGNED),
  20. 'extcredits3'=>array(R_INT, R_UNSIGNED),
  21. 'extcredits4'=>array(R_INT, R_UNSIGNED),
  22. 'extcredits5'=>array(R_INT, R_UNSIGNED),
  23. 'extcredits6'=>array(R_INT, R_UNSIGNED),
  24. 'extcredits7'=>array(R_INT, R_UNSIGNED),
  25. 'extcredits8'=>array(R_INT, R_UNSIGNED),
  26. 'email'=>array(R_STRING, 40)
  27. //列名不能以数字开头
  28. );

  29. function addSpecialToPack($ROW) {
  30. if($ROW==''){
  31. $GLOBALS['PACK'].=$GLOBALS['LastPack'].($GLOBALS['LastPackTimes']==1?''$GLOBALS['LastPackTimes']);
  32. $GLOBALS['LastPack']='';
  33. $GLOBALS['LastPackTimes']=0;
  34. return true;
  35. }
  36. if($GLOBALS['LastPack']==$ROW){
  37. $GLOBALS['LastPackTimes']++;
  38. }else{
  39. $GLOBALS['PACK'].=$GLOBALS['LastPack'].($GLOBALS['LastPackTimes']==1?''$GLOBALS['LastPackTimes']);
  40. $GLOBALS['LastPack']=$ROW;
  41. $GLOBALS['LastPackTimes']=1;
  42. }
  43. return true;
  44. }

  45. $PACK='';
  46. $LastPack='';
  47. $LastPackTimes='';
  48. $UNPACK=array();
  49. $TINY=array();
  50. foreach($Table as $Name => $Row){
  51. switch($Row[0]){
  52. case R_STRING:
  53. addSpecialToPack();
  54. $PACK.='a'.$Row[1];
  55. $UNPACK[]='a'.$Row[1].$Name;
  56. break;
  57. case R_HEX:
  58. addSpecialToPack('H');
  59. $UNPACK[]='H'.$Name;
  60. break;
  61. case R_SHORT:
  62. $MOD=($Row[1]==R_SIGNED)?'s':'n';
  63. addSpecialToPack($MOD);
  64. $UNPACK[]=$MOD.$Name;
  65. break;
  66. case R_INT:
  67. $MOD=($Row[1]==R_SIGNED)?'i':'I';
  68. addSpecialToPack($MOD);
  69. $UNPACK[]=$MOD.$Name;
  70. break;
  71. case R_LONG:
  72. $MOD=($Row[1]==R_SIGNED)?'l':'N';
  73. addSpecialToPack($MOD);
  74. $UNPACK[]=$MOD.$Name;
  75. break;
  76. default;
  77. break;
  78. }
  79. }

  80. print_r(array($PACK, implode('/', $UNPACK)));

  81. //三个组合,代入指定程序,几乎可实现无索引的表
  82. ?>
复制代码
Array ( [0] => Ia130HInI2NI7a40 [1] => Iuid/a13username/Hpassword/Iadminid/ngroupid/Ilastpost/Icredits/Nextcredits1/Iextcredits2/Iextcredits3/Iextcredits4/Iextcredits5/Iextcredits6/Iextcredits7/Iextcredits8/a40email )


返回列表 发新帖
 懒得打字嘛,点击右侧快捷回复【最新发布】   【赞助草根吧享更多权益】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

收藏帖子 返回列表 搜索

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

小黑屋|手机版|草根吧