note 15125 added to function.preg-replace
| From: | jasonmercer at yahoo dot com | Date: | Wed, 29 Aug 2001 01:47:17 +0000 |
| Subject: | note 15125 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-17282@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?
http://www.php.net/manual/en/function.preg-replace.php