regex - preg_split string with negative look-behind assertion php -
this array have
array ( [0] => string@:abc:-------:abc [1] => string@:abc:xyz-----:abc [2] => string1:string2:abc:xyz-----:abc [3] => string2:string3:abc:xyz-----:abc ) i want split these offset
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 ) ) my problem in 2,3 array string1 , string2 coming in diff offset want this
[0] => array ( [1st] => string1:string2 [2rd] => abc [3th] => xyz [4th]=> abc ) [1] => array ( [1st] => string2:string3 [2rd] => abc [3th] => xyz [4th]=> abc ) but there 2 special char @ nd : if in string1 @ not there want split start ,if @ not there want start after string2 please help
you can use negative lookbehind:
$arr = preg_split('/(?<!(?:string1|string2)):/', 'string1:string2:abc:xyz-----:abc'); var_dump($arr); output:
array(4) { [0]=> string(15) "string1:string2" [1]=> string(3) "abc" [2]=> string(8) "xyz-----" [3]=> string(3) "abc" }
Comments
Post a Comment