Splitting a string into quoted and unquoted sections

<?php
$str = 'unquoted section "quoted" and unquoted
    "another quoted \"with a subquote\" section" more unquoted';
preg_match_all('/"(?:\\\\.|[^"])*"|[^"]+/', $str, $m);
print_r($m);
?>

Output

Array
(
    [0] => Array
        (
            [0] => unquoted section
            [1] => "quoted"
            [2] =>  and unquoted
            [3] => "another quoted \"with a subquote\" section"
            [4] =>  more unquoted
        )

)

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.