Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Playing with strings Wish List | Report Bug | About Me ]

4.7.7     Trimming whitespace: trim(), ltrim(), and rtrim()

This is NOT the latest copy of this book; click here for the latest version.

string trim ( string source [, string charlist])

string ltrim ( string source [, string charlist])

string rtrim ( string source [, string charlist])

Trim() is a function to strip whitespace from either side of a string variable, with "whitespace" meaning spaces, new lines, and tabs. That is, if you have the string " This is a test " and pass it to trim() as its first parameter, it will return the string "This is a test" - the same thing, but with the spaces trimmed off the end.

You can pass an optional second parameter to trim() if you want, which should be a string specifying the characters you want it to trim(). For example, if we were to pass to trim the second parameter " tes" (that starts with a space), it would output "This is a" - the test would be trimmed, as well as the spaces. As you can see, trim() is again case sensitive - the T in "This" is left untouched.

Trim() has two minor variant functions, ltrim() and rtrim(), which do the same thing but only trim from the left and right respectively.

Here are some examples:

<?php
    $a
= trim(" testing ");
    
$b = trim(" testing ", " teng");
    
$c = ltrim(" testing ");
?>

$a will result in "testing", $b will result in "sti", and $c will result in "testing " - as expected, and not surprising because trim() et al are simple to use.





<< 4.7.6 Returning the first occurrence of a string: strstr() and stristr()   4.7.8 Wrapping your lines: wordwrap() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
z - 29 Aug 2008

Tom huh comments on this darn website go from bottom to top. What an odd way of putting things.

z - 29 Aug 2008

to Unix Programmer
you are missing two ;

to PHP User
>Why does in the second paragraph's example, the 's' is 'This' is not trimmed?

Tts
It trims only after it matches second t, in first s there is no t beforehand.
I guess, or something like that)) its odd..

Just use preg_replace or strtr or str_replace, they are much simpler to use. Or use trim in something trivial like $replace = trim($replace,"\n"); simple 'n nice.


BTW it seems that trim can use two or more options at the same time, via charlist option, can anyone show an example?
Or does it mean only a list, like \x00..\x1F ?

Thank you.

Tom - 29 Aug 2008

>Why does in the second paragraph's example, the 's' is 'This' is not trimmed?
That's because trim() and co only trim off the sides.
If you want to trim trailing spaces from a text document, try this:
<?php
//We're going to trim multiple lines
$foo = <<<TEXT
some stuff......
foo = bar.........
TEXT;
//Now we explode it into an array
$foo = explode("\n", $foo);

foreach($foo as $val) {
$string = rtrim($val, '.');
}
?>

JMH - 29 Aug 2008

I'm not exactly sure how that second parameter works...

Unix Programmer - 29 Aug 2008

A suggestion: It might help to show a case where letters in the second parameter are not removed because they are not at the ends of the string. For example,

$string = "This is a test"
$str = trim($string, " Tts");
print $str

will print the string "his is a te". Since it removes beginning and ending characters, it removes the 'T' in 'This', the last 't' in 'test' and the 's' (now at the end of the string) in 'tes'.

A PHP User - 29 Aug 2008

Why does in the second paragraph's example, the 's' is 'This' is not trimmed?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow