php tutorial - PHP trim function for removing spaces - php programming - learn php - php code - php script
Purpose of trim function in PHP
The PHP trim function is used to remove spaces or other characters like a tab, new line, a vertical tab etc. in a given string.
Sometimes, it is required to ensure that the text entered from the users does not contain leading or trailing whitespaces before storing into the database or displaying in an HTML element.
The trim PHP function can be easily used to accomplish that.
Syntax of trim function
This is how you may use the trim function in PHP programs:
The trim function returns the trimmed string. If you do not specify any characters in the second parameter, the trim function not only removes the single space but tabs, new lines etc.
See the following examples of using the trim function for removing spaces and other characters in the given string.
An example of using trim function
In this example, a string is created with leading and trailing spaces. After that, the trim function is used to remove those spaces.
The string is displayed before and after using the trim function.
Learn php - php tutorial - php-trim - php examples - php programs
An HTML span tag with the background color is used for displaying the strings before and after using the trim function to see the difference clearly.
The following PHP code is executed:
An example of using tab (\t) in a string
As mentioned earlier, if you do not specify the character mask in the trim function, it will not only remove the single space but if tab (\t), new line character (\n), a carriage return (\r) etc. are used in a string, they will also be removed while using the trim function.
In this example, leading and trailing spaces are added by using the \t character. After that, the trim function is used without specifying the character mask:
Learn php - php tutorial - php-trim-tabs - php examples - php programs
This is how the tabs are added in a string and trim function is used:
Note that, if you specify”\t” as character mask, it will also strip the tabs:
An example of removing left space only
If you require removing white spaces from the left of a string then you may use the PHP ltrim function. In this example, the source string contains space on both sides.
The ltrim function is used to remove the space from the left side of the string only. See the code and output:
Learn php - php tutorial - php-ltrim - php examples - php programs
You can see, the output string after using the ltrim function has removed the space from left only. The space at the right side is still visible.
Removing white space from the right side example
Similarly, PHP has the rtrim function for removing the white spaces from the right side of the string. A demo is shown in this example:
Learn php - php tutorial - php-rtrim - php examples - php programs
By using rtrim function:
The space from the right side is striped while it is still visible in the left side of the trimmed string.
An example of trim function with PHP arrays
- You may delete white spaces in PHP arrays as well by using the trim function. In this example, an array of three elements is created with leading and trailing spaces in each element.
- A PHP function is created where the trim function is used to delete spaces from both sides for each element.
- The array elements are displayed before and after using the trim function so you can see the difference. Just like above examples, the span tag is used to highlight element values so that spaces are clearly visible.
Learn php - php tutorial - php-trim-arrays - php examples - php programs
This is how a PHP function is created and trim function is used:
This code creates that array and calls the function where the foreach loop is used twice: