Re: split or explode quoted strings
| From: | Robin Vickery | Date: | Mon, 14 Nov 2005 16:03:27 +0000 |
| Subject: | Re: split or explode quoted strings | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-225740@lists.php.net to get a copy of this message | ||
On 11/14/05, Ördögh László <ordoghl@gmail.com> wrote:
> Hello,
>
> I would like to split or explode strings in a way that
> quoted strings inside the strings should remain.
> e.g.:
>
> "first second \"third third\" fourth \"fifth fifth fifth\""
>
> after the split I need:
>
> "first"
> "second"
> "third third"
> "fourth"
> "fifth fifth fifth"
How about something like this?
<?php
$singleQuoted = "(?:'(?:[^'\\\\]|\\\\.)*')";
$doubleQuoted = '(?:"(?:[^"\\\\]|\\\\.)*")';
$re = "/$singleQuoted|$doubleQuoted|\S+/s";
preg_match_all($re, $text, $quotedWords);
print_r($quotedWords);
?>