2016-11-08 80 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