Php preg_split last character occurrence

We are looking for help!

I need to split the line when the last space appears ...

eg. Great Neck NY I need to break it down so I have Great Neck and NY.

I had no problem using preg_split with basic materials, but I was at a standstill trying to figure out how to say this, only to break up in the latter case! Any help would be appreciated!

Mike

+4
source share
1 answer

You can use the lookahead statement :

preg_split('/\s+(?=\S+$)/', $str) 

Now the line will be split into \s+ (whitespace) only if (?=\S+$) matches this point. And \S+$ matches characters without spaces immediately at the end of the line.

+12
source

Source: https://habr.com/ru/post/1304282/


All Articles