php split string based on pattern

前端 未结 2 1697
甜味超标
甜味超标 2021-01-26 00:48

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,4         


        
2条回答
  •  天命终不由人
    2021-01-26 01:41

    You can use preg_split for that:

    $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));
    

    Your output will be

    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.

提交回复
热议问题