In PHP, if you have a variable with binary data, how do you get specific bytes from the data? For example, if I have some data that is 30 bytes long how do I get the first 8 bytes?
Right now, I'm treating it like a string, using the substr()
function:
$data = //...
$first8Bytes = substr($data, 0, 8);
Is it safe to use substr
with binary data?
Or are there other functions that I should be using?
If the mbstring extension is installed and mbstring overloading is enabled, then using substr
might give you trouble. Mbstring overloading will cause mb_substr
to be automatically called every time substr
is called (if mbstring is installed and mbstring overloading is disabled, then substr
will correctly retrieve the bytes). The following code will use mb_substr
if mbstring is installed and substr
if it isn't. The "8bit" character encoding is used, which will treat each character as 1 byte and will ignore null terminators ('\0').
if (function_exists('mb_substr')) {
$bytes = mb_substr($string, 0, 8, '8bit');
} else {
$bytes = substr($string, 0, 8);
}
Thanks to ircmaxell
Generally all string functions in PHP are safe to use with raw bytes. The issue that mostly crops up are null-bytes, but only for filesystem-functions: http://php.net/manual/en/security.filesystem.nullbytes.php
Your substr()
is perfectly fine to use with binary strings. Some other functions like strtok
and ereg
however interface to C, where the "\0" character becomes an issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With