php extract last n words of string

Solutions on MaxInterview for php extract last n words of string by the best coders in the world

showing results for - "php extract last n words of string"
Juliana
03 Jun 2018
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} */
Charlette
05 Oct 2016
1function getLastWord($string)
2    {
3        $string = explode(' ', $string);
4        $last_word = array_pop($string);
5        return $last_word;
6    }
queries leading to this page
string last value get in phpget last characters name phpget only last character of string phpphp get last character of stringphp check second last character of stringhow to find last 2 words in string phpcheck string last character phpif last element of string phpstring last word in phpsubstring last character phpphp last characters of stringfind last position of character in string phpcatch last character of string in phpphp string cut last characterget last word from the strin gin phpphp get 2 last word of stringget last char of array phpphp last character of a stringtake string from last 23 value in phpphp get last occeance of wordstring last character phpstrchr last character phpget last char in string phpphp get last wordphp get last n chars of stringget last 10 degit drom string phplast character of string phpphp get last 4 characters of stringget last 4 characters string phpstring last position phpphp check last word in a stringphp get last index of stringlast character string phpget word after last string phpget last letters from string phptake last five characters in a string in phpstr last value in phpget last string in phpsub last string in phpget the last character of a string phpphp last characterphp string last characterphp get last characterstake string from last value in phpphp get last second character of stringphp get last 2 wordphp check n last 3 characters of stringphp characters after last occurrence of a character in a stringphp get string last characterhow to get the last character opf a php stringphp extract last n words of stringphp get last two words in stringet last n character of string phpphp get last n characters of stringphp get last character in stringphp get string from middle to lastlast position string phpget last string until 2f phpget last chracter of str phplast word of string in phpget last 2 letters of string phpphp get last 5 characters of stringget the last string in phpget last character of string phpget last string before phpphp find last function before funtionsubstring from last phpphp get the last character of a stringphp get all characters except lastphp get last string characterget last 4 characters of string phpphp last word stringget last string after 2f in phpphp get last 4 of stringget last word in phpphp last element of stringcheck last character phpget part of string after last 2f phpcheck last character of string phpget last character of string in phpget last 2f from string 2b phpget last instance of string in string phpget the last char of string in phpget the last word from path in phpget last char of string phpget last name from string in phpcheck last char of string phpin string get the last int phpphp keep only last n char in stringphp get last char of stringphp get all but last character of stringphp substr get last charphp last word in stringphp last part of a stringphp get last part of stringphp string get last characterget string last position phpget last character of a string in phpphp get last character positionphp find last character in stringget last line phpphp substr from last characterhow to get the last character of a string phpphp get last word in stringphp last character of stringhow to get last few characters from a string in phpget last character from string phpget last word string phpget last word in a sentence phpphp string take 9 from last lettercheck last letter of string phphow take the last character of a string in phpphp extract last word from stringshow last characters from stirng phptake out till last after given string phpphp check last character of stringget last word from string phpphp return last n chars stringphp extract last n words of string