PHP Split string with exception -
i have string need split out :
3,1,'2015,05,14,11,18,0', 99
i want split
3 1 '2015,05,14,11,18,0' 99
how php ?
one of comments (@tuananh in particular) said csv parser
, little bit of trial, fgetcsv
work too, you'll got have temporary file holds simple string, unlink after operation.
just set enclosure single quotes when parser breaks up, gets whole string enclosed single quotes.
$string = "3,1,'2015,05,14,11,18,0', 99"; file_put_contents('temp.csv', $string); // create temporary file $fh = fopen('temp.csv', 'r'); // open $line = fgetcsv($fh, strlen($string) + 1, ',', "'"); // set enclosure single quotes fclose($fh); unlink('temp.csv'); // remove temp file print_r($line); // array ( [0] => 3 [1] => 1 [2] => 2015,05,14,11,18,0 [3] => 99 ) // echo implode("\n", $line);
sidenote: if indeed csv file, use fgetcsv
whole thing.
edit: @deceze said use csv function strings
there's thing called str_getcsv
, no need put inside file unlink whatsoever.
$string = "3,1,'2015,05,14,11,18,0', 99"; $line = str_getcsv($string, ',', "'"); // set enclosure single quotes print_r($line);
Comments
Post a Comment