preg_split字符串与负面的后台断言php

时间:2014-03-25 07:03:13

标签: php regex string split preg-match

这是我有的数组

 Array
    (

        [0] =>                  string@:abc:-------:abc
        [1] =>                  string@:abc:xyz-----:abc
        [2] =>               string1:string2:abc:xyz-----:abc                               
        [3] =>               string2:string3:abc:xyz-----:abc            
     )

我想像这样分割这些偏移

Array
(
    [0] => Array
        (
            [1st] =>                  String@
            [2nd] => abc
            [3rd] => ------
            [4th] => abc
        )

   [1] => Array
        (
            [1st] =>                  String@
            [2nd] => abc
            [3rd] => xyz
            [4th] => abc
        )

    [2] => Array
        (
            [1st] =>                  String1
            [2nd] => string2
            [3rd] => abc
            [4th] => xyz
            [5th]=> abc
        )

    [3] => Array
        (
            [1st] =>                  String2
            [2nd] => string3
            [3rd] => abc
            [4th] => xyz
            [5th]=> abc
        )

)

我的问题是在2,3数组string1和string2进入差异偏移我希望像这样

[0] => Array
        (
            [1st] =>     String1:string2
            [2rd] => abc
            [3th] => xyz
            [4th]=> abc
        )
  [1] => Array
        (
            [1st] =>     String2:string3
            [2rd] => abc
            [3th] => xyz
            [4th]=> abc
        )

但是只有两个特殊的char @ nd:如果在string1 @不存在我想从开始分割,如果@不存在我想在string2之后开始 请帮忙

1 个答案:

答案 0 :(得分:0)

你可以使用这种负面的外观:

$arr = preg_split('/(?<!(?:string1|string2)):/', 'string1:string2:abc:xyz-----:abc');
var_dump($arr);

<强>输出:

array(4) {
  [0]=>
  string(15) "string1:string2"
  [1]=>
  string(3) "abc"
  [2]=>
  string(8) "xyz-----"
  [3]=>
  string(3) "abc"
}