łańcuch do tablicy, podzielony przez pojedyncze i podwójne cudzysłowy
Usiłuję użyć php, aby podzielić ciąg na komponenty tablicowe używając albo"
lub'
jako separator. Chcę tylko podzielić się na skrajny łańcuch. oto cztery przykłady i pożądany wynik dla każdego:
$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
)*/
jak widać, chcę podzielić się tylko na najbardziej zewnętrzny cytat i chcę zignorować wszelkie cytaty w cudzysłowie.
najbliższy wymyśliłem$pattern
jest
$pattern = "/((?P<quot>['\"])[^(?P=quot)]*?(?P=quot))/";
ale oczywiście to nie działa.