I'm using the following code to split a string into an array of matches:
$text = 'text required name="first_name" label="First Name"';
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|[^\s"]+/', $text, $matches);
print_r($matches);
The result is:
Array
(
[0] => Array
(
[0] => text
[1] => required
[2] => name=
[3] => "first_name"
[4] => label=
[5] => "First Name"
)
)
But I need the result to be:
Array
(
[0] => Array
(
[0] => text
[1] => required
[2] => name="first_name"
[3] => label="First Name"
)
)
I tried this but it didn't work:
preg_match_all('/="(?:\\\\.|[^\\\\"])*"|[^\s"]+/', $text, $matches);
Can anyone tell me where I'm going wrong? Thanks
explode(' ',$string);|which implies to search for either any text that is between quotation marks or any text that is not a space or double quote. You would be better served either by anexplodeto simply split text values or if you really need regex for whatever reason something that is similar to:([^\s"]+(="\w*")*). I'm a bit exhausted so it may not be exactly that but more or less should give you a jumping point.