I’m trying to split a string received from a $_GET in PHP, but I’m stuck, as it’s more than the explode() function will handle – or so I think.
If the string I receive contains quotations marks (“”), I want the split (by spaces) to keep the stuff in quotation marks intact. E.g. A result like ‘Foo bar “Bar 2″‘ would be split into 1 –> Foo ; 2 –> bar ; 3 –> Bar 2 .
I’ve looked through explode(), preg_split, and similar things, but I don’t understand how to split a larger string by space, whilst keeping intact substrings that are in quotation marks.
Thanks.
That’s the definition of a CSV file with space as the delimiter. Use the CSV parsing function
str_getcsv.Output:
For PHP < 5.3, from the PHP manual’s comments: