php根据模式分割字符串 [英] php split string based on pattern

查看:91
本文介绍了php根据模式分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的字符串:

I have a string like this:

2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;4534453,23,23,44,433;
3,23,44,433;23,23,44,433;23,23,44,433
7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433

如您所见,值之间存在分号.我想基于仅在7位数字值前的分号"分割此字符串,所以我应该拥有这个.

As you see there are semicolons between values. I want to split this string based on 'only semicolons before 7 digits values' so I shoud have this.

>2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433
>4534453,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;
>7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433

我唯一能想到的是explode(';',$string),但这返回了这一点:

the only thing that I can think of is explode(';',$string) but this returns this:

>2234323,23,23,44,433;
>3,23,44,433;
>23,23,44,433;
>23,23,44,433
>4534453,23,23,44,433;
>3,23,44,433;
>23,23,44,433;23,23,44,433;
>7545455,23,23,44,433;
>3,23,44,433;23,23,44,433;
>23,23,44,433

是否有任何快速方法可以基于;"分割具有这种格式的字符串在7位数字之前?

Is there any fast method to split string with this format based on the ";" before 7 digits values??

推荐答案

您可以为此使用preg_split:

$s = '2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;4534453,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433';
var_dump(preg_split('/(;\d{7},)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE));

您的输出将是

array(5) {
  [0] =>
  string(58) "2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433"
  [1] =>
  string(9) ";4534453,"
  [2] =>
  string(50) "23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433"
  [3] =>
  string(9) ";7545455,"
  [4] =>
  string(50) "23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433"
}

我认为接下来的事情(结合第一,第二,第三和第四元素)不是什么大问题:)

I think that the next thing (combine the 1st and 2nd and then 3rd and 4th elements) is not a big deal :)

让我知道您是否仍然在这里遇到问题.

Let me know if you still here problems here.

这篇关于php根据模式分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆