Jump to content

[SOLVED] Split by space except where within "?


stuartmarsh

Recommended Posts

$str = 'Split this "string by spaces"';
$arr = preg_split('#(?:\s+|("[^"]+"))#', $str, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
echo "<pre>".print_r($arr, true);

 

output (via <pre> and print_r)

Array
(
    [0] => Split
    [1] => this
    [2] => "string by spaces"
)

 

If you don't want it output like that, and just want the results without arry info, simply use a foreach instead:

 

foreach($arr as $val){
echo $val . "<br />\n";
}

 

Output:

Split
this
"string by spaces"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.