PHP program to find the length of the last word in string Last Updated : 24 May, 2022 Comments Improve Suggest changes Like Article Like Report We are given a string. We are required to write a program in PHP to find the length of the last word in the string using inbuilt functions. We have already discussed approach of solving this problem here. This article discusses the PHP solution to the problem. Examples: Input : "php exercises" Output : 9 Input : "geeks for geeks" Output : 5 We will mainly use these three inbuilt function in PHP to solve this problem: substr() Function: This inbuilt function in PHP is used to extract a part of string.strrpos() Function: This inbuilt function in PHP is used to find the last position of string in original or in another string. It returns the integer value corresponding to position of last occurrence of the string, also it treats uppercase and lowercase characters uniquely.strlen() Function: This inbuilt function in PHP is used to find the length of a string. The idea to solve this problem using the above mentioned inbuilt function is to first find the position of last occurring space in the string using the strrpos() function. After getting the position of last occurring space we can easily get the last word in the string using the substr() function and store this in a new string variable. At last, we can use the strlen() function to find the length of the last word in the string. PHP <?php // PHP code to find the length of last word // in a string // function to find length of last word function length_last_word($string) { // position of last occurring space // in the string $pos = strrpos($string, ' '); // if the string has only one word if(!$pos) { $pos = 0; } else { $pos = $pos + 1; } // get the last word in the string $lastWord = substr($string,$pos); // return length of last word return strlen($lastWord); } // Driver Code print_r(length_last_word('geeksforgeeks')."\n"); print_r(length_last_word('computer science portal')."\n"); ?> Output: 13 6 Comment More infoAdvertise with us Next Article PHP program to find the length of the last word in string A akash1295 Follow Improve Article Tags : Misc Web Technologies PHP PHP-function Practice Tags : Misc Similar Reads PHP | Print the last value of an array without affecting the pointer We are given an array with key-value pair, and we need to find the last value of array without affecting the array pointer. Examples: Input : $arr = array('c1' => 'Red', 'c2' => 'Green', 'c3' => 'Blue', 'c4' => 'Black') Output : Black Input : $arr = array('p1' => 'New York', 'p2' = 2 min read How to limit string length in PHP ? A string is a sequence of characters in PHP. The length of the string can be limited in PHP using various in-built functions, wherein the string character count can be restricted. Table of ContentUsing for loopUsing mb_strimwidth() function Using substr() method Using Regular Expressions with preg_m 6 min read How to remove the first character of string in PHP? Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation:In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. re 3 min read How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in 2 min read How to get the position of character in a string in PHP ? In this article, we will get the position of the character in the given string in PHP. String is a set of characters. We will get the position of the character in a string by using strpos() function. Syntax: strpos(string, character, start_pos) Parameters: string (mandatory): This parameter refers t 2 min read Like