PHP Str_getcsv implements the method of parsing strings into arrays

Source: Internet
Author: User
Tags explode parse string
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.


Related Article

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.