使用php在字符串中按整数分割字符串到数组


Splitting strings in to array by intergers in the string using 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.
) 
有谁能帮我吗?谢谢你。

您可以使用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);

您可以在preg_match_all()中使用regex来选择字符串的目标部分。

选择两个数字之间的字符串。
preg_match_all("/'d+([^'d]+)/", $string, $matches);
print_r($matches[1]);

参见demo