Php str_getcsv parses string into an array implementation method, str_getcsv Array

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.