In PHP, you can use the str_split() function to convert a connected string into a character array. This function can split the string according to the specified length of the array element, split the string into substrings of the specified length, and pass them into the array one by one as array elements; you only need to set the second parameter of the function to 1 , or omit it to convert the continuous string into a character array, the syntax is "str_split(string,1)" or "str_split(string)".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
Character array is used to store characters or strings. An element in the character array stores a character, which occupies one byte in memory. Example
#So in PHP, how to convert a continuous string into a character array?
In PHP, you can use the str_split() function to convert a continuous string into a character array.
str_split(string,length)
string: Required. Specifies the string to be split.
#length: Optional. Specifies the length of each array element. The default is 1.
If length is less than 1, the str_split() function will return FALSE. If length is greater than the length of the string, the entire string will be returned as the only element of the array.
str_split() function can split the string according to the specified length of the array element, split the string into substrings of the specified length, and pass them into the array one by one as an array element.
You only need to set the second parameter of the str_split() function to 1, or omit it to convert the continuous string into a character array.
Example: Convert the string "hello123" to a character array
<?php header("Content-type:text/html;charset=utf-8"); $str="hello123"; echo "原字符串:"; var_dump($str); echo "字符串转字符数组:"; $arr1=str_split($str,1); var_dump($arr1); $arr2=str_split($str); var_dump($arr2); ?>
It can be seen that regardless of whether the second parameter of the str_split() function is set to 1 , or you can omit it.
Extended knowledge: explode() function--convert a string into an array
explode($delimiter, $string [, $limit])
' '.
<?php header('content-type:text/html;charset=utf-8'); $str = 'hypertext language programming'; var_dump($str); $arr=explode(" ",$str); var_dump($arr); ?>
PHP Video Tutorial"
The above is the detailed content of How to convert connected strings into character arrays in php. For more information, please follow other related articles on the PHP Chinese website!