The following small series for everyone to bring a PHP str_getcsv string parsing to the implementation of the array. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
PHP uses the explode method to parse string parsing into arrays based on delimiters.
For example: Use "," to parse a string into an array of delimiters
<?php$str = ' explode '; $arr = (', ', $str);p Rint_r ($arr);? >
Output:
Array ( [0] = 1 [1] = 2 [2] = 3)
But for some more complex strings, such as the CSV format, using explode does not produce the desired results, and using the regular is cumbersome.
For example:
<?PHP$STR = "China, Guangdong Province, Guangzhou, Tianhe District, ' 113.329884,23.154799 ', 1, ' 2016-01-01 12:00:00 ', ' 1,2,3,4,5,6 '"; $arr = Explode (', ', $ STR);p Rint_r ($arr);? >
Output:
Array ( [0] = China [1] = Guangdong province [2] = Guangzhou [3] = Tianhe District [4] = = 113.329884 [5] = 23.1 54799 ' [6] = 1 [7] = ' 2016-01-01 12:00:00 ' [8] = ' 1 [9] = 2 [Ten] = 3 [11] = > 4 [[] = 5 [] + 6 ')
In fact, the desired result is:
Array ( [0] = China [1] = Guangdong province [2] = Guangzhou [3] = Tianhe District [4] = 113.329884,23.154799< C21/>[5] = 1 [6] = 2016-01-01 12:00:00 [7] = 1,2,3,4,5,6)
PHP provides the str_getcsv method for processing strings as a CSV format, which makes it easy to parse the array.
str_getcsv parsing a CSV string as an array
Array str_getcsv (String $input [, String $delimiter = "," [, String $enclosure = ' "' [, string $escape =" \ \ "]])
Parameters:
input string to parse
delimiter set field qualifiers (single character only)
Enclosure Set field wrap character (single character only)
Escape Sets the escape character (only a single character), and the default is a backslash (\)
Instance:
<?PHP$STR = "China, Guangdong Province, Guangzhou, Tianhe District, ' 113.329884,23.154799 ', 1, ' 2016-01-01 12:00:00 ', ' 1,2,3,4,5,6 '"; $arr = Str_getcsv ($str , ', ', ' ' ');p rint_r ($arr);? >
Output:
Array ( [0] = China [1] = Guangdong province [2] = Guangzhou [3] = Tianhe District [4] = 113.329884,23.154799 [5] = 1 [6] = 2016-01-01 12:00:00 [7] = 1,2,3,4,5,6)
The above is the whole content of this article, I hope that everyone's study has helped.