unpack Unpack data from binary string
发表日期:2021-07-01 10:15:44 | 来源: | | 浏览(1175) 分类:杂项 函数
unpack
(PHP 4, PHP 5, PHP 7, PHP 8)
unpack — Unpack data from binary string
说明
$format
, string $string
, int $offset
= 0): array|false
Unpacks from a binary string into an array according to the given
format
.
The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name.
Changes were made to bring this function into line with Perl:
- The "a" code now retains trailing NULL bytes.
- The "A" code now strips all trailing ASCII whitespace (spaces, tabs, newlines, carriage returns, and NULL bytes).
- The "Z" code was added for NULL-padded strings, and removes trailing NULL bytes.
参数
-
format
-
See pack() for an explanation of the format codes.
-
string
-
The packed data.
-
offset
-
The offset to begin unpacking from.
返回值
Returns an associative array containing unpacked elements of binary
string, 或者在失败时返回 false
.
更新日志
版本 | 说明 |
---|---|
7.2.0 | float and double types supports both Big Endian and Little Endian. |
7.1.0 |
The optional offset has been added.
|
范例
示例 #1 unpack() example
<?php $binarydata = "\x04\x00\xa0\x00"; $array = unpack("cchars/nint", $binarydata); print_r($array); ?>
以上例程会输出:
Array ( [chars] => 4 [int] => 160 )
示例 #2 unpack() example with a repeater
<?php $binarydata = "\x04\x00\xa0\x00"; $array = unpack("c2chars/nint", $binarydata); print_r($array); ?>
以上例程会输出:
Array ( [chars1] => 4 [chars2] => 0 [int] => 40960 )
注释
Note that PHP internally stores integral values as signed. If you unpack a large unsigned long and it is of the same size as PHP internally stored values the result will be a negative number even though unsigned unpacking was specified.
If you do not name an element, numeric indices starting from 1
are used.
Be aware that if you have more than one unnamed element, some data is
overwritten because the numbering restarts from 1
for each element.
示例 #3 unpack() example with unnamed keys
<?php $binarydata = "\x32\x42\x00\xa0"; $array = unpack("c2/n", $binarydata); var_dump($array); ?>
以上例程会输出:
array(2) { [1]=> int(160) [2]=> int(66) }
Note that the
first value from the c
specifier is
overwritten by the first value from the n
specifier.
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- connection_aborted 检查客户端是否已经断开(0)
- connection_status 返回连接的状态位(0)
- constant 返回一个常量的值(0)
- define 定义一个常量(0)
- defined 检查某个名称的常量是否存在(0)
- die 等同于 exit()(0)
- eval 把字符串作为PHP代码执行(0)
- exit 输出一个消息并且退出当前脚本(0)
- get_browser 获取浏览器具有的功能(0)
- __halt_compiler 中断编译器的执行(0)
- highlight_file 语法高亮一个文件(0)
- highlight_string 字符串的语法高亮(0)
- hrtime 获取系统的高精度时间(0)
- ignore_user_abort 设置客户端断开连接时是否中断脚本的执行(0)
- pack 将数据打包成二进制字符串(0)
- php_strip_whitespace 返回删除注释和空格后的PHP源码(0)
- sapi_windows_cp_conv Convert string from one codepage to another(0)
- sapi_windows_cp_get Get current codepage(0)
- sapi_windows_cp_is_utf8 Indicates whether the codepage is UTF-8 compatible(0)
- sapi_windows_cp_set Set process codepage(0)
- sapi_windows_generate_ctrl_event Send a CTRL event to another process(0)
- sapi_windows_set_ctrl_handler Set or remove a CTRL event handler(0)
- sapi_windows_vt100_support Get or set VT100 support for the specified stream associated to an output buffer of a Windows console.(0)
- show_source 别名 highlight_file()(0)
- sleep 延缓执行(0)
- sys_getloadavg 获取系统的负载(load average)(0)
- time_nanosleep 延缓执行若干秒和纳秒(0)
- time_sleep_until 使脚本睡眠到指定的时间为止。(0)
- uniqid 生成一个唯一ID(0)
- unpack Unpack data from binary string(0)
- usleep 以指定的微秒数延迟执行(0)
- 字符串 函数(101)