In PHP programming, sometimes we need to convert a string into a byte array to facilitate binary data processing. In this article, we will explain how to convert a string into a byte array using PHP.
- Use str_split() function
PHP’s str_split() function can split a string into an array, where each element is a single character (ASCII code). When using this function, you need to pay attention to character encoding issues. For example, if the string is UTF-8 encoded, each Chinese character typically requires 3 bytes or more of storage space.
The following is a sample code:
// 定义一个字符串 $str = 'hello world'; // 将字符串转换为字节数组 $bytes = array_map('ord', str_split($str));
In the above code, we first define a string ("hello world"), and then use the str_split() function to split it into an array. Next, we use PHP's built-in ord() function to convert each character to ASCII code, and store the converted array in the $bytes variable.
- Use the unpack() function
PHP’s unpack() function can unpack binary data into an array or associative array in a specified format. This function is relatively powerful and can support unpacking in multiple formats, such as structures in C language, etc.
The following is a sample code:
// 定义一个字符串 $str = 'hello world'; // 将字符串转换为字节数组 $bytes = unpack('C*', $str);
In the above code, we first define a string ("hello world"), and then use the unpack() function to unpack it into C language character type ('C'). Finally, the converted byte array is stored in the $bytes variable.
- Use the mb_str_split() function
If the string is a multi-byte encoding (such as UTF-8), the above method may produce wrong results because of the multi-byte Section characters need to be split according to their encoding format.
To solve this problem, you can use the mb_str_split() function to process multi-byte strings. This function is similar to the str_split() function, but supports multi-byte characters.
The following is a sample code:
// 定义一个多字节字符串 $str = '今天天气真好'; // 将字符串转换为字节数组 $bytes = array_map('ord', mb_str_split($str));
In the above code, we first define a multi-byte string ("The weather is really nice today"), and then use the mb_str_split() function to split it Split into multiple characters, and finally use the ord() function to convert each character into ASCII code, and store the converted array in the $bytes variable.
Summary
In PHP programming, when you need to convert a string into a byte array, you can use methods such as str_split(), unpack() or mb_str_split() functions. Among them, choosing the appropriate method needs to take into account many factors such as the encoding format of the string and the format of the binary data. In actual development, it is necessary to choose an appropriate method based on specific needs.
The above is the detailed content of php convert string to byte array. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
