PHP unpack() Function
Example
Unpack data from a binary string:
<?php
$data = "PHP";
print_r(unpack("C*",$data));
?>
Try it Yourself »
Definition and Usage
The unpack() function unpacks data from a binary string.
Syntax
unpack(format,data)
Parameter Values
Parameter | Description |
---|---|
format | Required. Specifies the format to use when unpacking data. Possible values:
|
data | Required. Specifies the binary data to be unpacked |
offset | Optional. Specifies where to start unpacking from. Default is 0. |
Technical Details
Return Value: | Returns an array on success, or FALSE on failure. |
---|---|
PHP Version: | 4+ |
Changelog: | PHP 7.2 - float and double now supports both big and small endian. PHP 7.1 - Added the optional offset parameter. PHP 5.5.0 - The following changes were made for Perl compatibility: The "a" code now retains trailing NULL bytes. The "A" code now strips all trailing ASCII whitespace. The "Z" code was added for NULL-padded strings, and removes trailing NULL bytes. |
More Examples
Example
Unpack data:
<?php
$bin = pack("c2n2",0x1234,0x5678,65,66);
print_r(unpack("c2chars/n2int",$bin));
?>
Try it Yourself »
❮ PHP Misc Reference