How do I split a string on two different positions with two different delimiters where the first is a whitespace, and that the whitespace can only be the very first occurence of it?.
The first split is after the very first word,
and the second split is based on a spesific word.
$string = 'BMW 220 2.0i 184Hk/270Nm TUNE Stage R 262Hk/425Nm';
I tried preg_split()
with two words.
list($first,$middle,$last) = preg_split('/( ORG | TUNE )/',$string);
This apporach means that I would have to provide a spesific word after the first to make the split work.
So how can I make this work with a whitespace as the first delimiter, and only the very first occurence of it. Then split it at the defined word?
I did came up with this solution though, but not sure if that's the best approach?
list($first,$string)=explode(' ', $string, 2);
list($middle,$last) = explode(' TUNE ',$string);
$first = BMW
$middle => 220 2.0i 184Hk/270Nm
$last => Stage R 262Hk/425Nm
It doesn't have to be a split , does it?
preg_match('/^(\S+)\s+(.+)WORLD(.+)/', $string, $matches);
$matches[1]
is now everything before the first space, $matches[2]
everything between the space and "WORLD", and $matches[3]
everything after it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.