Php str_getcsv parses string into an array implementation method, str_getcsv Array
Php parses strings into arrays based on delimiters.ExplodeMethod implementation
For example, use "," as the delimiter to parse the string as an array
<?php$str = '1,2,3';$arr = explode(',', $str);print_r($arr);?>
Output:
Array( [0] => 1 [1] => 2 [2] => 3)
However, for some complex strings, such as csv format, it is troublesome to use regular expressions instead of using explode to obtain desired results.
For example:
<? Php $ str = "China, Guangdong, Guangzhou, Tianhe District, '2017. 329884, 23.154799 ', 1, '1970-01-01 12:00:00', '1, 2016, 6 '"; $ arr = explode (', ', $ str ); print_r ($ arr);?>
Output:
Array ([0] => China [1] => Guangdong [2] => Guangzhou [3] => Tianhe District [4] => '2017. 329884 [5] => 23.154799 '[6] => 1 [7] => '2017-01-01 12:00:00' [8] => '1 [9] => 2 [10] => 3 [11] => 4 [12] => 5 [13] => 6 ')
The expected result is:
Array ([0] => China [1] => Guangdong [2] => Guangzhou [3] => Tianhe District [4] => 113.329884, 23.154799 [5] => 1 [6] => 12:00:00 [7] =>)
Php providesStr_getcsvYou can use the string as the csv format to parse it into an array.
Str_getcsvParses csv strings into Arrays
array str_getcsv ( string $input [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\\" ]]] )
Parameters:
Input string to be parsed
Delimiter sets the field delimiter (only one character)
Enclosure sets the character of the field package (only one character)
Set escape characters (only one character) for escape. The default value is backslash (\)
Instance:
<? Php $ str = "China, Guangdong, Guangzhou, Tianhe District, '2017. 329884, 23.154799 ', 1, '1970-01-01 12:00:00', '1, 2016, 6 '"; $ arr = str_getcsv ($ str ,',', "'"); print_r ($ arr);?>
Output:
Array ([0] => China [1] => Guangdong [2] => Guangzhou [3] => Tianhe District [4] => 113.329884, 23.154799 [5] => 1 [6] => 12:00:00 [7] =>)
In the above php str_getcsv article, the implementation method of parsing strings into arrays is all the content shared by the editor. I hope you can provide a reference and support for the help house.