Development php

How to Trim Strings in PHP

The function of trimming strings is to remove the unwanted characters from string data.  String data requires trimming for various purposes in PHP programming. Some use of trimming is removing unnecessary spaces from user data, eliminating newline character from the end of the string data etc.  When the user submits data by using form fields then unwanted spaces can be submitted with the data by mistake. This type of mistake will generate the error when the data is used for validation. So string trimming is essential for many cases. Three functions are used in PHP for string trimming. These are trim(), ltrim() and rtrim(). The use of these functions is shown in this tutorial using examples.

All trimming functions are used to remove any whitespaces from the starting and end of the string. The following characters are considered as whitespace in PHP. The particular character range can be used without these whitespace characters.

“ “        - space character

“\n”     - newline character

“\t”      - tab character

“\r”      - carriage return character

“\0”     - vertical tab character

“\x0B” – null-byte character

Example-1: trim() function

trim() function can take two inputs as argument values. First argument is mandatory and second argument is optional.  If the optional argument value is omitted then the function will remove space from the starting and ending part the value provided by the first argument. In the following example, there are multiple spaces at the starting and the ending of the variable $str1 and trim function is applied for $str1 without the optional parameter.  “\r\n” whitespaces are included in the variable $str2 and “\r\n” is used as second argument value of trim() function. In this case, trim() function will remove all “\r\n” whitespaces from both side of the variable $str2. Range values can be used as second argument value. There are numeric values on both side of the variable $str3 and numeric range 0..9 is used as second argument value of trim() function to remove numeric part from both side of the variable $str3.

$str1 = "     I like programming   ";

//using trim() function without optional value
echo trim($str1)."<br/>";
$str2 = "\r\nPHP is a popular programming language\r\n";

//using trim() function with “\r\n” as optional value
echo trim($str2, "\r\n")."<br/>";
$str3 = "123 890";

//using trim() function with numeric range as optional value
echo trim($str3, "0..9")."<br/>";


Example-2: ltrim() and rtrim() functions

ltrim() and rtrim() functions are identical to trim() function. ltrim() removes whitespaces or other specific characters from the left side and rtrim() removes  whitespaces or other specific characters from the right side of any string value. In the following example, ltrim() and rtrim() are applied on two variables, $Str1 and $Str2 with and without optional value.

$Str1 = "      PHP is a popular language     ";
$Str2 = "02JavaScript is client-side scripting language99";

//using ltrim() function
echo "Output of ltrim() without optional value: <b>". ltrim( $Str1 )."</b><br/>";
echo "Output of ltrim() with optional value: <b>". ltrim( $Str2, "0..9" )."</b><br/><br/>";

//using rtrim() function
echo "Output of rtrim() without optional value: <b>". rtrim( $Str1 )."</b><br/>";
echo "Output of rtrim() with optional value: <b> ". rtrim( $Str2, "0..9" )."</b><br/>";



You will need to use trimming functions in your PHP program based on the requirement for getting the appropriate output.  I hope this tutorial will help you to understand the use of trimming functions and apply them properly in your PHP script.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.