Input : PHP Exercises Write a PHP program to find the length of the last word in a string. Explanation : Sample Solution : PHP Code :
Sample Output: 9 Single word Blank String Blank String Flowchart: PHP Code Editor: Contribute your code and comments through Disqus. Previous: Write a PHP program to find majority element in an array. What is the difficulty level of this exercise? Test your Programming skills with w3resource's quiz. PHP: Tips of the DayPHP: Pass a PHP string to a JavaScript variable (and escape newlines) <script> var myvar = <?php echo json_encode($myVarValue); ?>; </script> Using json_encode() requires:
Since UTF-8 supports full Unicode, it should be safe to convert on the fly. Note that because json_encode escapes forward slashes, even a string that contains </script> will be escaped safely for printing with a script block. Ref : https://bit.ly/34f0Dym
View Discussion Improve Article Save Article View Discussion Improve Article Save Article 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:
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
Output: 13 6 How can I get the last word of a string in PHP?The strrpos() function finds the position of the last occurrence of a string inside another string.
How do you find the length of a word in a string?Java String length(). Signature. The signature of the string length() method is given below: public int length() ... . Specified by. CharSequence interface.. Returns. Length of characters. In other words, the total number of characters present in the string. ... . Internal implementation. public int length() { return value.length;. How do I count words in PHP?PHP str_word_count() Function. Count the number of words found in the string "Hello World!": echo str_word_count("Hello world!"); ... . Return an array with the words from the string: ... . Return an array where the key is the position of the word in the string, and value is the actual word: ... . Without and with the char parameter:. How do I cut a string after a specific character in PHP?The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.
|