perl-esque data parsing for hex post variables
| From: | Jason Mercer | Date: | Wed, 29 Aug 2001 02:13:04 +0000 |
| Subject: | perl-esque data parsing for hex post variables | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-64930@lists.php.net to get a copy of this message | ||
I can't get the php to convert audio data to nicely formed binary wav
format. For instance, in perl you simply do a
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
and you're done with it.
In php, there are two problems:
1. no existence of a handy builtin hex2bin() function (though many have been
written, such as
function hex2bin($hex_str)
for ($i = 0; $i < strlen($hex_str); $i += 2)
$bin_str .= chr(hexdec(substr($hex_str, $i, 2)));
}
return $bin_str;
}
which seems to get the case done,
2. But the real problem is that doing a straight hex2bin conversion is not
what that beautiful little perl script does. The part that confuses me is
the hash oriented piece /%([a-fA-F0-9][a-fA-F0-9])/ -- what exactly is it
doing? Is it just stepping through the string two characters at a time and
comparing them? Or is it doing something more ambitious and arcane that I'm
not really grasping. I tried to emulate it with the following php
workaround, but come up short...
for ($i = 0; $i < strlen($hex_str); $i += 2)
$newdata =.
preg_replace("/[a-fA-F0-9][a-fA-F0-9]/",pack("C",hexdec(substr($hex_str, $i,
2))),$hex_str);
}
Any help?