Conversion method: 1. Use explode() to split the string into several substrings according to specific delimiters, and then combine the substrings into an array; the syntax format is "explode(separator, string)". 2. Use str_split() to split the string into an array, the syntax format is "str_split(string)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
numeric characters in php String to array
1. Use the explode() function
During the development process, we often encounter the need to convert a string into an array Case. PHP has a built-in explode() function that can split a string into several parts based on specific delimiters.
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. The syntax format is as follows:
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, 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, 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, it means returning all array elements.
If $delimiter is an empty string""
, the program will prompt a Warning, and the explode() function will return FALSE; if If the value contained in $delimiter is not found in $string, and a negative $limit is used, an empty array will be returned, otherwise an array containing a single element of $string will be returned.
<?php $str = '544527258895525404524.54434245657'; echo '<pre class="brush:php;toolbar:false">'; $arr = explode('2', $str); print_r($arr); $arr = explode('2', $str, 3); print_r($arr); $arr = explode('2', $str, -2); print_r($arr); $arr = explode('2', $str, 0); print_r($arr); $arr = explode('', $str); var_dump($arr); $arr = explode('@', $str, -1); print_r($arr); ?>
Output result:
[Recommended learning: "PHP Video Tutorial"]
2. Use the str_split() function
str_split() function to split the string into an array.
Syntax
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.
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 is returned as the only element of the array.
Example:
<?php $str = '536546'; echo '<pre class="brush:php;toolbar:false">'; $arr = str_split($str); print_r($arr); $arr = str_split($str,3); print_r($arr); ?>
Output result:
##For more programming related knowledge, please visit:Programming Video ! !
The above is the detailed content of How to convert numeric string to array in php. For more information, please follow other related articles on the PHP Chinese website!