In PHP, strings are a very common data type, and string length issues are often dealt with. In some cases, we may need to convert a string into a byte array to better handle the contents of the string. This article will introduce how to convert a string into an array by bytes.
In PHP, strings are enclosed in single quotes or double quotes, and characters are enclosed in a pair of single quotes or double quotes. Here is a simple string example:
$str = "Hello World!";
In PHP, a string is an array of characters, each character can be accessed by index. For example, we can use the following code to access the first character of the above example string:
echo $str[0]; // 输出“H”
This method ensures that the string is accessed by characters instead of bytes. However, in some cases, we need to access the string according to bytes. In this case, we need to convert the string into a byte array.
To achieve this purpose, PHP provides several built-in functions, including str_split() and mb_str_split(). Below we introduce how these two functions convert strings into byte arrays.
Use the str_split() function
The str_split() function splits a string into a set of characters and stores them in an array. The following is an example code that uses the str_split() function to convert a string into an array by bytes:
$str = "Hello World!"; $byteArray = str_split($str, 1); print_r($byteArray);
The output result is:
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => W [7] => o [8] => r [9] => l [10] => d [11] => ! )
In the above example, we convert the string $str Each character in is split into a separate array element and the result is stored in the $byteArray array. The second parameter represents the length of each element, here set to 1, indicating splitting into single characters. It should be noted that the second parameter of the str_split() function can be omitted and defaults to 1.
Use the mb_str_split() function
Like the example above, str_split() can only split a string by bytes. However, in some cases, one character may occupy multiple words. section, at this time, incorrect splitting may occur for Chinese and other characters. To avoid this situation, PHP provides the mb_str_split() function, which can split strings according to character length. Before using the mb_str_split() function, you need to ensure that PHP has the mbstring extension installed. You can check whether the extension is available through the phpinfo() function. The following is an example code that uses the mb_str_split() function to convert a string into an array by bytes:
$str = "你好,世界!"; $byteArray = mb_str_split($str); print_r($byteArray);
The output result is:
Array ( [0] => 你 [1] => 好 [2] => , [3] => 世 [4] => 界 [5] => ! )
In the above example, we use the mb_str_split() function to convert $ Each character in str is split into a separate array element and the result is stored into the $byteArray array. This function does not need to provide a second argument because it automatically splits the string based on the number of bytes in the characters.
Summary
This article introduces how to convert a string into an array by bytes in PHP, including the use of str_split() and mb_str_split() functions. These functions can help us better process the content of strings and lay the foundation for subsequent string processing.
The above is the detailed content of How to convert php string to array based on bytes. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)