How to take last word from string in php?
View Discussion Show
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 to remove the last word from a string in php? Are you searching for How to remove the last word from a string in php? Then you are in the right place. Sometimes we need to remove the last word from a string in php for various purposes. We can do it in many ways. In this GeekySolution, I will show you How to remove the last word from a string in php? The technique which I am going to show you here is really very easy but powerful. Also if you want, you can modify and customize these code easily according to your need and make it more powerful. In this GeekySolution, you will learn...
Time needed: 2 minutes. To remove the last word from a string in php just follow the steps below:
Complete code to remove the last word from a string in PHP:To remove the last word from a string using php just use the code below: How to use this code?Insert the above php code to your php file. Common problems and solutions about: “How to remove the last word from a string in php?”How to remove the last word from String in php?GeekySolution:To remove the last word from String in php just use this code- How to use this code?Insert the above php code to your php file. How to remove the last word from a string in php?GeekySolution:To remove the last word from a string in php just use this code- How to use this code?Insert the above php code to your php file. How to remove the last word from a string in php?GeekySolution:To remove the last word from a string in php just use this code- How to use this code?Insert the above php code to your php file. How to strip whitespace or other characters from the end of a string in php?GeekySolution:To strip whitespace (or other characters) from the end of a string in php just use this code- "; // Specify the string $str2 = "GeekySolution is awesome"; // Remove extra white space at the right and show it echo "String after removal of character or word at the end or right: " . rtrim($str2,"awesome"); ?> How to use this code?Insert the above php code to your php file. How to find the length of the last word in string in php?GeekySolution:To find the length of the last word in string in php just use this code- "; // Last word start position $last_word_start = strrpos ( $str , " ") + 1; // Last word end position $last_word_end = strlen($str) - 1; // Get the last word $last_word = substr($str, $last_word_start, $last_word_end); // Show the last word echo "The last word is: " . $last_word . " How to use this code?Insert the above php code to your php file. How to remove the last word from a string in php?GeekySolution:To remove the last word from a string in php just use this code- How to use this code?Insert the above php code to your php file. References:
You are valuable to usIf you like this post then share it on Facebook, Twitter or any other social network with your friends so that other people can get help from this post. If you have any question or suggestion about this post then write a comment below.
Hi, I'm James. I'm a technology lover. Knowledge is power. So always I try to learn in any situation from anyone. How can I get the first word of a string in PHP?echo "The first word of string is: " . $arr [0]; ?> Method 3: Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string.
What is use of trim in PHP?Definition and Usage. The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.
How can I remove last 5 characters from a string in PHP?You can use the PHP substr_replace() function to remove the last character from a string in PHP. $string = "Hello TecAdmin!"; echo "Original string: " . $string .
|