php strings can be converted into arrays. 5 conversion methods: 1. Use "str_split (string, element length)" to convert the string into an array; 2. Use "explode (separator, string)" to convert the string into an array; 3. Use "preg_split" ('match pattern', string, -1, $flags)" to convert; 4. Use "settype (string, "array")" to convert; 5. Add parentheses before the string variable Target type "(array)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
The string in php can be converted into array. In php, there are many ways to convert strings into arrays.
Method 1: Use the str_split() function
The str_split() function splits the string into an array, that is, converts the string into a character array.
str_split(string,length)
The parameter description is as follows:
string Required. Specifies the string to be split.
#length Optional. Specifies the length of each array element. The default is 1.
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello'; echo "原字符串:"; var_dump($str); echo "将字符串转为数组:"; $arr1=str_split($str); var_dump($arr1); $arr2=str_split($str,2); var_dump($arr2); ?>
Method 2: Use the explode() function
explode() function can be based on strings The delimiter splits the string, 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 delimiter character used to split the string;
- $string: The string that needs to be split;
-
$limit: Optional parameter, 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, and 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 $limit is 0, it will be treated as 1;
- If $limit is empty, all array elements are returned.
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = 'hypertext language programming'; echo "原字符串:"; var_dump($str); echo "将字符串转为数组(分割符为空格):"; $arr=explode(" ",$str); var_dump($arr); ?>
Method 3. Use the preg_split() function--- Convert the string into an array
preg_split() function splits the string through a regular expression.
preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
The parameter description is as follows:
- $pattern: The pattern used for matching, that is, the regular expression.
- $subject The string to be split.
- $limit: Optional parameter. If specified, the substrings obtained by limiting the separation will be limited to at most limit, and the last substring will contain all remaining parts. When the limit value is -1, 0 or NULL, it means "no limit". It is recommended to use NULL.
- $flags: optional parameter, which has 3 values.
- If set to PREG_SPLIT_NO_EMPTY, preg_split() will return the separated non-empty part.
- If set to PREG_SPLIT_DELIM_CAPTURE, bracket expressions in delimited patterns will be captured and returned.
- If set to PREG_SPLIT_OFFSET_CAPTURE, the string offset will be appended to the return for each occurrence of a match.
Note: This will change each element in the returned array so that each element becomes an array consisting of the 0th element being the separated substring and the 1st element being the offset of the substring in the subject. .
Return value: Returns an array composed of substrings obtained after using $pattern to split the subject string.
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = '1 2 3 4,5 6-7 8=9'; var_dump($str); $arr=preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE); var_dump($arr); ?>
Method 4: Use the settype() function
The settype() function can re Set the type of the variable and convert the type of the string variable to an array.
settype($var,$type)
The parameter description is as follows:
$var: the variable to be converted.
$type: required data type.
Just set $type to ""array"" to set the variable $var to the array type.
Note: The settype() function will change the original variable, returning TRUE if the setting is successful and FALSE if it fails.
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello world!'; echo "原字符串:"; var_dump($str); echo "将字符串转为数组类型:"; $arr=settype($str,"array"); var_dump($str); var_dump($arr); ?>
Method 5: Add the target type "(array)" enclosed in brackets before the string variable
(array): Force the specified variable into an array type;
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello world!'; echo "原字符串:"; var_dump($str); echo "将字符串转为数组类型:"; $arr=(array)$str; var_dump($arr); ?>
推荐学习:《PHP视频教程》
The above is the detailed content of Can strings be converted into arrays in php?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools
