Hello,
I'm working on a template class and I've an issue when trying to parse out a list of quoted strings from a string argument list. Take for example the string:
$string = 'VAR_SELECTED, \'Hello m\'lady\', "null"';
I'm having a problem coming up with a regex that extracts the string "Hello m'lady" and "null". The closest I have got is
$string = 'VAR_SELECTED, \'Hello m\'lady\', "null", \'TE\'ST\'';
preg_match_all('/(?:[^\']|\\\\.)+|(?:[^"]|\\\\.)+/', $string, $matches);
print_r($matches);
Which outputs:
Array
(
[0] => Array
(
[0] => VAR_SELECTED,
[1] => 'Hello m'lady',
[2] => "null",
[3] => 'TE'ST'
)
)
However a more complex case of:
$string = 'VAR_SELECTED, \'Hello "Father"\', "Hello \'Luke\'"';
preg_match_all('/(?:[^\']|\\\\.)+|(?:[^"]|\\\\.)+/', $string, $matches);
print_r($matches);
outputs:
Array
(
[0] => Array
(
[0] => VAR_SELECTED,
[1] => 'Hello
[2] => "Father"
[3] => ',
[4] => "Hello
[5] => 'Luke'
[6] => "
)
)
Can anyone help me solve this problem? Are multiple regexes the way forward?
Edit Maybe it would be easier to replace the commas within the strings with a placeholder and then break apart the strings with an explode?
Edit 2 Just thought of a simple insecure option (that I am not going to use), but generates an E_NOTICE error.
$string = 'return array(VAR_SELECTED, \'Hello , "Father"\', "Hello \'Luke\'4");';
$string = eval($string);
print_r($string);