Home > Backend Development > PHP Problem > How to convert php string to array based on bytes

How to convert php string to array based on bytes

PHPz
Release: 2023-04-26 14:50:49
Original
600 people have browsed it

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!";
Copy after login

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”
Copy after login

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);
Copy after login

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] => !
)
Copy after login

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);
Copy after login

The output result is:

Array
(
    [0] => 你
    [1] => 好
    [2] => ,
    [3] => 世
    [4] => 界
    [5] => !
)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template