I've been trying to figure out if the PHP implementation of Pack/Unpack can do something that the Perl version is able to do. The example I'd like to be able to do in PHP is:
http://perldoc.perl.org/perlpacktut.html#String-Lengths
# pack a message: ASCIIZ, ASCIIZ, length/string, byte
my $msg = pack( 'Z* Z* C/A* C', $src, $dst, $sm, $prio );
# unpack
( $src, $dst, $sm, $prio ) = unpack( 'Z* Z* C/A* C', $msg );
What this Perl code does is described as:
Combining two pack codes with a slash (/) associates them with a single value from the argument list. In pack, the length of the argument is taken and packed according to the first code while the argument itself is added after being converted with the template code after the slash.
i.e. you can pack variable length strings and then unpack them in one step, rather than having to figure out the length of the string first and then extracting it as a separate step.
The PHP manual doesn't mention this capability and any attempt to insert a pattern like 'C/A*' into unpack is giving errors for me. Is that an oversight in the manual or just something that the PHP version doesn't support?
a
orZ
. They don't do anything. The documentation even mentions them being updated to work like Perl. ?! This is a continuing issue even in PHP 8. – Toxoid