Trimming PHP Strings
Explains PHP's trim(), ltrim() and rtrim() functions for removing unwanted characters from the beginning and/or end of a string.
In programming-speak, trimming a string means removing unwanted characters — typically whitespace characters — from either end (or both ends) of the string. This is useful in a number of scenarios, such as:
- Cleaning up user input, such as text field values in a submitted form
- Removing unwanted line-break characters at the end of a line
- Ensuring that a string is in the correct format for passing to another program
In this article you explore 3 useful PHP functions that you can use to trim strings:
trim()for trimming both ends of a string
ltrim()for trimming the beginning of a string, and
rtrim()for trimming the end of a string.
Trimming a string with
trim() function removes all whitespace from the beginning and the end of a string. Here's an example:
$myString = " Hello there! "; // Displays "Hello there!" echo trim( $myString );
Notice that PHP doesn't remove the space between
"there!". Only whitespace at the start and end of a string are removed.
What is whitespace?
PHP considers the following characters to be whitespace:
- The space character (
- The tab character (
- The vertical tab character (
- The newline character (
- The carriage return character (
- The null-byte character (
$myString = " \t\tHello there!\r\n\r\n"; // Displays "Hello there!" echo trim( $myString );
If you need to trim other characters from the string, you can specify a list of characters to trim as the second argument to
trim(). The supplied list replaces the default list of characters. For example, you might want to trim carriage returns and newlines but leave in spaces:
$myString = "\r\r Hello there! \n\n"; // Displays " Hello there! " echo trim( $myString, "\r\n" );
You can specify a range of characters using 2-dot (
..) notation. The following example trims all space characters and digits:
$myString = "123 Hello there! 456"; // Displays "Hello there!" echo trim( $myString, " 0..9" );
Trimming either end of a string with
rtrim() functions work much like
trim(), except that they only trim one end of the string:
ltrim()removes whitespace only from the start of the string, leaving any whitespace at the end of the string intact.
rtrim()does the opposite: it only removes whitespace from the end of the string.
Here are a couple of examples using
$myString = " Hello there! "; // Displays "Hello there! " echo ltrim( $myString ); $myString = "123 Hello there! 456"; // Displays "123 Hello there!" echo rtrim( $myString, " 0..9" );
rtrim() in particular is handy for stripping end-of-line characters from a string that was read from a file:
$myString = "Hello there!\r\n"; // Displays "Hello there!" echo rtrim( $myString, "\r\n" );
In this article you've looked at PHP's
rtrim() functions for removing unwanted characters around a string. You also learned about whitespace characters, and you saw how to specify your own list of whitespace characters when trimming.
Learn PHP With Ease!
Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:
...and lots more!
“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz
Responses to this article
There are no responses yet.
Post a response
Want to add a comment, or ask a question about this article? Post a response.
To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.