String zum Anordnen, durch einfache und doppelte Anführungszeichen getrennt
Ich versuche, PHP zu verwenden, um eine Zeichenfolge mit entweder in Array-Komponenten aufzuteilen"
oder'
als Begrenzer. Ich möchte nur durch die äußerste Zeichenfolge teilen. Hier sind vier Beispiele und das gewünschte Ergebnis für jedes:
$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
)*/
Wie Sie sehen, möchte ich nur durch das äußerste Zitat teilen, und ich möchte alle Zitate innerhalb der Zitate ignorieren.
das nächste, das ich mir ausgedacht habe$pattern
ist
$pattern = "/((?P<quot>['\"])[^(?P=quot)]*?(?P=quot))/";
aber offensichtlich funktioniert das nicht.