PHP中的字节操作 [英] Byte manipulation in PHP

查看:110
本文介绍了PHP中的字节操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,如果您有一个带有二进制数据的变量,那么如何从数据中获取特定的字节?例如,如果我有一些30字节长的数据,如何获得前8个字节?

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?

现在,我使用substr()函数将其视为字符串:

Right now, I'm treating it like a string, using the substr() function:

$data = //...
$first8Bytes = substr($data, 0, 8);

对二进制数据使用substr是否安全?

Is it safe to use substr with binary data?

还是我应该使用其他功能?

Or are there other functions that I should be using?

推荐答案

通常,PHP中的所有字符串函数都可以安全地与原始字节一起使用.主要出现的问题是空字节,但仅用于文件系统功能: http ://php.net/manual/zh-CN/security.filesystem.nullbytes.php

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

您的substr()非常适合与二进制字符串一起使用.其他一些功能,例如strtokereg,却与C相连,在这里C变成了"\ 0"字符.

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.

这篇关于PHP中的字节操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆