I am using the solution from 2202435. But when I add brackets to the string, it doesn't give the right result in the array.
$text = 'Lorem ipsum ("dolor sit amet") consectetur "adipiscing \\"elit" dolor';
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $text, $matches);
print_r($matches);
The above code produces
Array
(
[0] => Array
(
[0] => Lorem
[1] => ipsum
[2] => ("dolor
[3] => sit
[4] => amet")
[5] => consectetur
[6] => "adipiscing \"elit"
[7] => dolor
)
)
But the result I am looking for is
Array
(
[0] => Array
(
[0] => Lorem
[1] => ipsum
[2] => (
[3] => "dolor sit amet"
[4] => )
[5] => consectetur
[6] => "adipiscing \"elit"
[7] => dolor
)
)
I am able to achieve the above result, if I include a space after '( ' and before ' )'.
Please advise the correct regex expression that would allow me to keep the brackets seperate (with explanation if possible).
Thank you.