I have a string like "Tom Jerry ". I want to trim the extra space from the end of the string. I.e. desirable output is "Tom Jerry". How do I do that?
Asked
Active
Viewed 1.0k times
1
-
3just use `trim( "Tom Jerry ")` function is used to remove the white spaces – JYoThI Aug 16 '17 at 10:00
-
1You need to use `trim()` – Milan Chheda Aug 16 '17 at 10:01
-
1possible duplicated :https://stackoverflow.com/questions/1101322/trim-leading-white-space-with-php – Mr Alb Aug 16 '17 at 10:08
-
Possible duplicate of [Trim leading white space with PHP?](https://stackoverflow.com/questions/1101322/trim-leading-white-space-with-php) – apaderno Mar 12 '18 at 09:50
7 Answers
6
There is a trim
method in php: http://php.net/manual/en/function.trim.php
trim("Tom Jerry ");

Dan Mason
- 2,177
- 1
- 16
- 38
4
use the trim(string) function ??

user8224713
- 41
- 2
-
4Would be better if you pointed to the official PHP Docs rather than a 3rd party site – Mark Baker Aug 16 '17 at 10:06
4
You should use rtrim instead. It will remove extra white space at the end of a string and is faster than using preg_replace.
$str = "This is a string. ";
echo rtrim($str);
source : Remove extra space at the end of string using preg_replace

Dinum Dissanayaka
- 386
- 2
- 9
1
for a string $string = " my name is Stark ";
use ltrim($string)
to trim whitespace from the beginning of the string, use rtrim($string)
to trim whitespace from the end of the string, use trim($string)
to remove whitespace from both ends. you can also add an extra parameter (a character mask) if you want to specifically remove another character also, apart from whitespace from the end or beginning of a string e.g trailing slashes(" onefunnyurl.com/"
)

bestbrain10
- 151
- 6