2016-11-08 52 views
1

我有這個示例字符串。使用php將字符串拆分爲數組中的字符串

$string = "There four of them. These are: 1 The first one. Its is the most common. 2 The second one. 3 The third one. 4 This is the last."; 

我想拆分成數組,其中包含上面$string中給出的信息。我希望它看起來像這樣。

Array ( 
    [0] => The first one. Its is the most common. 
    [1] => The second one. 
    [2] => The third one. 
    [3] => This is the last. 
) 

有什麼可以幫助我嗎。謝謝。

+0

爆炸(,$字符串 「」); –

+0

但第一個數組包含兩個點(。)s – Jote

+0

請標記一個答案來完成此討論。 – Mohammad

回答

3

可以使用preg_split的整數,分割字符串,例如:

$string = "There four of them. These are: 1 The first one. Its is the most common. 2 The second one. 3 The third one. 4 This is the last."; 

$matches = preg_split('/\d+/', $string); 

var_dump($matches); 
+0

謝謝。有效。它計數字符的數量。但我用戶print_r()。謝謝。 – Jote

+0

但是'$ matches [0]'是另外的。 – Mohammad

1

您可以使用正則表達式中preg_match_all()選擇字符串的目標的一部分。正則表達式選擇兩個數字之間的字符串。

preg_match_all("/\d+([^\d]+)/", $string, $matches); 
print_r($matches[1]); 

參見導致demo