In PHP, you can use the str_split() function to transfer words into an array of letters; this function can split the string according to the specified length and pass it into the array. When the split length is 1, it can be Split the word into letters and transfer them to the letter array; syntax "str_split(word,1)" or "str_split(word)".
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
php converts words into letters In the array
Transfer the word into the letter array, which means dividing the word into letters and passing them into the letter array as elements one by one.
To put it simply, converts a word (a string) into a character array, and the function is str_split()
function.
<?php header("Content-type:text/html;charset=utf-8"); $str="hello"; echo "原单词:".$str; $arr=str_split($str); echo "<br><br>字母数组:"; var_dump($arr); ?>
str_split() function introduction
str_split() function splits a string into an array.
Syntax:
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.
Return value:
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.
Example:
<?php header("Content-type:text/html;charset=utf-8"); var_dump(str_split("Hello")); var_dump(str_split("Hello",1)); var_dump(str_split("Hello",2)); ?>
Extended knowledge: Another function to convert string to array explode()
The explode() function can split a string based on the string delimiter, that is, it splits a string into several substrings based on the delimiter, and then combines these substrings into an array and returns it.
explode($delimiter, $string [, $limit])
The parameter description is as follows:
Just set $delimiter to ' '
.
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = 'hypertext language programming'; var_dump($str); $arr=explode(" ",$str); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert words into alphabetical array in php. For more information, please follow other related articles on the PHP Chinese website!