簡體   English   中英

使用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()使用正則表達式來選擇字符串的目標部分。 正則表達式選擇兩個數字之間的字符串。

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

演示中查看結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM