我需要用preg_split
生成一個數組,因爲implode('', $array)
可以重新生成原始字符串。 `的如何在preg_split(PHP)中的一對第二分隔符內停止分割?
$str = 'this is a test "some quotations is her" and more';
$array = preg_split('/(|".*?")/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
使preg_split生成的
Array
(
[0] => this
[1] =>
[2] => is
[3] =>
[4] => a
[5] =>
[6] => test
[7] =>
[8] =>
[9] => "some quotations is here"
[10] =>
[11] =>
[12] => and
[13] =>
[14] => more
)
我需要的空間的護理之前/引號太后,生成與所述原始字符串的具體圖案的陣列的陣列。
例如,如果該字符串是test "some quotations is here"and
,陣列應該是
Array
(
[0] => test
[1] =>
[2] => "some quotations is here"
[3] => and
)
注:基於與@mikel初始討論編輯已經取得進展。
完美的作品!我在正則表達式上有一點創意:) – Googlebot
對不起,我有這個解決方案的一個小問題。它將在引號前後添加雙倍空間(第一個分隔符)。事實上,它將同時與兩個分隔符合作。 – Googlebot
噢,我編輯了一個新版本,你可以試試嗎? – mikel