Home >Backend Development >PHP Problem >How to convert words into alphabetical array in php

How to convert words into alphabetical array in php

青灯夜游
青灯夜游Original
2023-01-05 20:23:363871browse

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)".

How to convert words into alphabetical array in php

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);
?>

How to convert words into alphabetical array in php

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));
?>

How to convert words into alphabetical array in php

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:

  • $delimiter: the splitting character used to split the string;
  • $string: the string that needs to be split;
  • $limit: Optional parameter, which can be empty, specifies the number of array elements to be returned;
    • If $limit is not empty and is a positive number, the returned array contains at most $limit elements. The last element contains the remainder of $string;
    • If $limit is not empty and is negative, all elements except the last $limit elements are returned;
    • If $ If limit is 0, it will be treated as 1;
    • If $limit is empty, it means returning all array elements.

Just set $delimiter to ' '.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;hypertext language programming&#39;;
var_dump($str);
$arr=explode(" ",$str);
var_dump($arr);
?>

How to convert words into alphabetical array in php

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!

Statement:
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