Skip to content

Instantly share code, notes, and snippets.

@Servana
Created July 13, 2011 11:08
Show Gist options
  • Save Servana/1080107 to your computer and use it in GitHub Desktop.
Save Servana/1080107 to your computer and use it in GitHub Desktop.
Split string by spaces exception on spaces in braces and quotes
$str = '3021e69d9d2569db27ce7b74ccaa98496007de09a1f4b94e45f48203d8727ac6 test.com [05/Jul/2011:07:28:57 +0000] 126.97.98.19 3021e69d9d2569db27ce7b74ccaa98496007de09a1f4b94e45f48203d8727ac6 70706DF22A809F33 REST.GET.OBJECT 1as21/foldera/file.png "GET //folder/test.php?AWSAccessKeyId=45dsgrg$5f&Expires=1310388787&Signature=Yehn847BhdsaiuernYmjne= HTTP/1.1" 206 - 47972352 209316737 54194091 7 "http://test.info/?url=http://ns.in/54325" "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1" -';
$regex = <<<REGEX
/"((?:[^"]*|)++)\"
|\[((?:[^\]]*|)++)\]
|[\s]
/x
REGEX;
$out = preg_split($regex,$str, 0, PREG_SPLIT_DELIM_CAPTURE );
var_dump($out);
/**
array(27) {
[0]=>
string(64) "3021e69d9d2569db27ce7b74ccaa98496007de09a1f4b94e45f48203d8727ac6"
[1]=>
string(8) "test.com"
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(26) "05/Jul/2011:07:28:57 +0000"
[5]=>
string(0) ""
[6]=>
string(12) "126.97.98.19"
[7]=>
string(64) "3021e69d9d2569db27ce7b74ccaa98496007de09a1f4b94e45f48203d8727ac6"
[8]=>
string(16) "70706DF22A809F33"
[9]=>
string(15) "REST.GET.OBJECT"
[10]=>
string(22) "1as21/foldera/file.png"
[11]=>
string(0) ""
[12]=>
string(109) "GET //folder/test.php?AWSAccessKeyId=45dsgrg$5f&Expires=1310388787&Signature=Yehn847BhdsaiuernYmjne= HTTP/1.1"
[13]=>
string(0) ""
[14]=>
string(3) "206"
[15]=>
string(1) "-"
[16]=>
string(8) "47972352"
[17]=>
string(9) "209316737"
[18]=>
string(8) "54194091"
[19]=>
string(1) "7"
[20]=>
string(0) ""
[21]=>
string(40) "http://test.info/?url=http://ns.in/54325"
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(67) "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
[25]=>
string(0) ""
[26]=>
string(1) "-"
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment