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:
- 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
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...
- How to remove the last word from a string using php?
- Complete code to remove the last word from a string in PHP:
- 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?
- How to remove the last word from a string in php?
- How to remove the last word from a string in php?
- How to strip whitespace or other characters from the end of a string in php?
- How to find the length of the last word in string in php?
- How to remove the last word from a string in php?
- You are valuable to us
Time needed: 2 minutes.
To remove the last word from a string in php just follow the steps below:
- Specify the string from which we want to remove
Specify the string from which we want to remove using this code:
$str1 = ‘GeekySolution is awesome’; - Remove the last string
Remove the string using this code:
echo preg_replace[‘/\W\w+\s[\W]$/’, ‘$1’, $str1].”\n”;
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-
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-
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:
- //stackoverflow.com/questions/29428459/php-how-can-i-remove-from-string-the-last-word
- //www.w3resource.com/php-exercises/php-regular-expression-exercise-2.php
- //codereview.stackexchange.com/questions/8683/remove-last-word-from-string
- //www.codeproject.com/Questions/838096/How-can-I-remove-last-word-from-a-string
- //phpcorelab.com/php-remove-last-word-from-string/
You are valuable to us
If 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.
- Author
- Recent Posts
Hi, I'm James. I'm a technology lover.
I like to learn about modern technologies. I like software programming, web development, graphics design, and blogging.
Knowledge is power. So always I try to learn in any situation from anyone.