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!