In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "php how to convert a string into an array", the content is detailed, the steps are clear, and the details are handled properly. I hope this "php how to convert a string into an array" article can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.
Conversion methods: 1, use explode (), use spaces as splitters to split strings and convert them into arrays, syntax "explode (", $str) "; 2, use" preg_split ('/ /', $str,-1,PREG_SPLIT_OFFSET_CAPTURE) "statement.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
There are three methods built into php to convert strings to arrays:
Str_split () function
Explode () function
Preg_split () function
Among them, the str_split () function divides the string with the character length as the boundary, and passes the substring into the array
The explode () and preg_split () functions split the string with the specified character as the boundary, passing the substring into the array.
So you can use the explode () and preg_split () functions to convert strings to arrays bounded by spaces.
1. Use the explode () function
The explode () function splits a string based on a string splitter, that is, it splits a string into several substrings based on the delimiter, and then combines these substrings into an array and returns.
Explode ($delimiter, $string [, $limit])
The parameters are described as follows:
$delimiter: the split character used to split the string
$string: the string to be split
$limit: optional parameter, which can be empty to specify the number of array elements to be returned
If $limit is not empty and is positive, the returned array contains at most $limit elements, and the last element contains the rest of $string
If $limit is not empty and is negative, all elements except the last $limit element are returned
If $limit is 0, it will be treated as 1
If $limit is empty, all array elements are returned.
Just set $delimiter to''.
Example:
2. Use the preg_split () function
The preg_split () function splits strings through a regular expression.
Preg_split (string $pattern, string $subject [, int $limit =-1 [, int $flags = 0]])
The parameters are described as follows:
$pattern: the pattern used for matching, that is, the regular expression.
The string to be split by $subject.
$limit: optional parameter, if specified, will restrict the delimited substrings to a maximum of limit, and the last substring will contain all the rest. Limit values of-1, 0 or NULL all represent "unlimited" and it is recommended that you use NULL.
$flags: optional parameter, which has 3 values.
Note: this will change each element in the returned array so that each element becomes an array of substrings separated by the 0th element and the first element is the offset of the substring in the subject.
If set to PREG_SPLIT_NO_EMPTY, preg_split () returns the delimited non-empty part.
If set to PREG_SPLIT_DELIM_CAPTURE, parenthetical expressions in delimited patterns are captured and returned.
If set to PREG_SPLIT_OFFSET_CAPTURE, a string offset is appended when each occurrence of the match is returned.
Return value: returns an array of substrings obtained by splitting the subject string with $pattern.
Just set $pattern to / /.
Example:
Read here, this "php how to convert strings into arrays" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.