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 stringltrim()
for trimming the beginning of a string, andrtrim()
for trimming the end of a string.
Trimming a string with trim()
PHP’s 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 );
What is whitespace?
PHP considers the following characters to be whitespace:
- The space character (
" "
) - The tab character (
"\t"
) - The vertical tab character (
"\v"
) - The newline character (
"\n"
) - The carriage return character (
"\r"
) - The null-byte character (
"\x0B"
)
For example:
$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 ltrim()
and rtrim()
PHP’s ltrim()
and 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 ltrim()
and rtrim()
:
$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 trim()
, ltrim()
and 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.
Happy coding!
typing kick says
actually I am looking for trimming a newline from anywhere in the paragraph