You can use a lookahead assertion:
$string = 'DZ9243/XSHAGT FFGD JERSE XS2 DZ9232/MHAGT SUUMTE KNI M10 DZ9232/LHAGT SUMMER KNI L6';
$pieces = preg_split('@ (?=[^ ]*/)@', $string);
print_r($pieces);
The output is:
Array
(
[0] => DZ9243/XSHAGT FFGD JERSE XS2
[1] => DZ9232/MHAGT SUUMTE KNI M10
[2] => DZ9232/LHAGT SUMMER KNI L6
)
The regex
@ (?=[^ ]*/)@
@
is the regex
delimiter; usually /
is used as delimiter but this regex attempts to match /
and this is why it's better to use a different delimiter;
- it is followed by a space character; it is the space you want to use as delimiter for splitting the input string;
(
starts a group; the group is needed by the assertion;
?=
is forward looking positive assertion; it requires the group to match the input string but it does not consume the characters from the input string that matches the group;
[^ ]*/
is the group content; it matches any non-space character any number of times, followed by a /
; this is the word that contains the slash (/
);
)
ends the group.
All in all, the regex
matches the spaces that are followed by a word that contain a slash but the word is not consumed; it is not included in the delimiter by preg_split()
, only the space character is used.