I need to explode my string input into an array at the commas. However the string contains commas inside quotes.
Input:
$line = 'TRUE','59','A large number is 10,000'; $linearray = explode(",",$line); $linemysql = implode("','",$linearray);
Returns $linemysql as:
'TRUE','59','A large number is 10','000'
How can I go about accomplishing this, with the explode ignoring the commas inside the quote marks?
The task is to split the given string with comma delimiter and store the result in an array. Use explode() or preg_split() function to split the string in php with given delimiter. PHP | explode() Function: The explode() function is an inbuilt function in PHP which is used to split a string in different strings.
The comma separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function.
Since you are using comma seperated values, you can use str_getcsv
.
str_getcsv($line, ",", "'");
Will return:
Array ( [0] => TRUE [1] => 59 [2] => A large number is 10,000 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With