Byte manipulation in PHP

爷,独闯天下 提交于 2019-12-18 14:13:09

问题


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?


回答1:


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.




回答2:


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




回答3:


Sounds good since PHP is dealing strings (internally) "like" C char * (1byte=1char)

On the other side, it could be broken if the string is in Unicode encoding (2 bytes = 1 character)

nb: You can also play with pack() and unpack() to manipulate "real" bytes



来源:https://stackoverflow.com/questions/4570812/byte-manipulation-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!