Home >Backend Development >PHP Problem >What is the function to convert string to array in php
The functions in php that convert strings to arrays include: 1. str_split() function, syntax "str_split(string,length)"; 2. explode() function, which can return a string array; 3. preg_split() function.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Convert a string to an array in PHP The functions used are str_split(), explode(), preg_split().
PHP str_split() function
str_split() function splits a string into an array.
Syntax
str_split(string,length)
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 $str="Hello"; var_dump($str); $arr=str_split($str); var_dump($arr); ?>
PHP explode() function
explode() function uses a String splits another string and returns an array of strings.
Syntax
explode(separator,string,limit)
Return value: Returns a string array.
Example:
<?php $str="Hello world !"; var_dump($str); $arr=explode(" ",$str); var_dump($arr); ?>
PHP preg_split() function
preg_split function separates characters by a regular expression string.
Syntax
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
Separate the given string by a regular expression.
Parameter description:
Return value
Returns an array composed of substrings obtained after using pattern boundaries to separate subject.
Example:
<?php $str = 'hypertext language programming'; var_dump($str); $chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE); var_dump($chars); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the function to convert string to array in php. For more information, please follow other related articles on the PHP Chinese website!