строка в массив, разделенная одинарными и двойными кавычками
я пытаюсь использовать php для разделения строки на компоненты массива, используя либо"
или же'
в качестве разделителя. Я просто хочу разделить на крайнюю строку. Вот четыре примера и желаемый результат для каждого:
$pattern = "?????";
$str = "the cat 'sat on' the mat";
$res = preg_split($pattern, $str);
print_r($res);
/*output:
Array
(
[0] => the cat
[1] => 'sat on'
[2] => the mat
)*/
$str = "the cat \"sat on\" the mat";
$res = preg_split($pattern, $str);
print_r($res);
/*output:
Array
(
[0] => the cat
[1] => "sat on"
[2] => the mat
)*/
$str = "the \"cat 'sat' on\" the mat";
$res = preg_split($pattern, $str);
print_r($res);
/*output:
Array
(
[0] => the
[1] => "cat 'sat' on"
[2] => the mat
)*/
$str = "the 'cat \"sat\" on' the mat 'when \"it\" was' seventeen";
$res = preg_split($pattern, $str);
print_r($res);
/*output:
Array
(
[0] => the
[1] => 'cat "sat" on'
[2] => the mat
[3] => 'when "it" was'
[4] => seventeen
)*/
Как вы можете видеть, я хочу разделить только самые внешние цитаты, и я хочу игнорировать любые цитаты в цитатах.
ближе всего я придумал для$pattern
является
$pattern = "/((?P<quot>['\"])[^(?P=quot)]*?(?P=quot))/";
но, очевидно, это не работает.