By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

Split by spaces but not when phrase is inside quotes...

P: n/a
I'm writing a function that parses a nested list string that might look
like this:
( "HELLO WORLD!" 1231231 awesome ( 1 2 ) )

I wrote the logic already and it starts by splitting the string by the
space character (or tab or newline).
ex... $tklist = preg_split("/\s|\n|\t/", $str);

This works except in the string case, where "HELLO WORLD!" should not
be two parts. How do I split by spaces but keep phrases inside quotes
in tact? Is there an easy way to do this?

Thanks,
Mike

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
<af********@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I'm writing a function that parses a nested list string that might look
like this:
( "HELLO WORLD!" 1231231 awesome ( 1 2 ) )

I wrote the logic already and it starts by splitting the string by the
space character (or tab or newline).
ex... $tklist = preg_split("/\s|\n|\t/", $str);

This works except in the string case, where "HELLO WORLD!" should not
be two parts. How do I split by spaces but keep phrases inside quotes
in tact? Is there an easy way to do this?


Remove the substrings surrounded by quotes first. Then don't parse the
phrases that were enclosed - only split the rest of the string.
Jul 17 '05 #2

P: n/a
Brilliant. Thanks.

Mike

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.