How can I split a delimited string into an array in PHP?

前端 未结 10 1664
失恋的感觉
失恋的感觉 2020-11-21 11:11

I need to split my string input into an array at the commas.

How can I go about accomplishing this?

Input:

9,admin@example.com,8
10条回答
  •  情话喂你
    2020-11-21 11:46

    What if you want your parts to contain commas? Well, quote them. And then what about the quotes? Well, double them up. In other words:

    part1,"part2,with a comma and a quote "" in it",part3

    PHP provides the https://php.net/str_getcsv function to parse a string as if it were a line in a CSV file which can be used with the above line instead of explode:

    print_r(str_getcsv('part1,"part2,with a comma and a quote "" in it",part3'));
    Array
    (
        [0] => part1
        [1] => part2,with a comma and a quote " in it
        [2] => part3
    )
    

提交回复
热议问题