1<?php
2
3$str = "NAME WITH SPACES FIELD1 FIELD2 FIELD3 FIELD4";
4
5preg_match("/(\S+)\s(\S+)\s(\S+)\s(\S+)$/", $str, $matches);
6
7var_dump($matches);
8
9/* array(5) {
10 [0] => string(27) "FIELD1 FIELD2 FIELD3 FIELD4"
11 [1] => string(6) "FIELD1"
12 [2] => string(6) "FIELD2"
13 [3] => string(6) "FIELD3"
14 [4] => string(6) "FIELD4"
15} */
1function getLastWord($string)
2 {
3 $string = explode(' ', $string);
4 $last_word = array_pop($string);
5 return $last_word;
6 }