PHP trim() function is mainly used to trim or remove whitespace from the start and end of the string. We can also use it in various ways which we learn from this complete article.
PHP trim() function is an inbuilt PHP function. There are also two types of trim functions,
- rtrim removes whitespaces and other specified characters from the right side of the string.
- ltrim removes whitespaces and other specified characters from the left side of the string.
Syntax
1 |
trim ( string $string , string $characters = " \n\r\t\v\0" ) : string |
The syntax trim()
function accepts two parameters,
$string
is the string that we want to trim.$characters
is an optional argument, used to remove specific characters from beginning and end of the string.
The output of the trim function is also a String.
There are also some default characters that the trim function removes,
Character | ASCII | Hex | Description |
---|---|---|---|
” “ | 32 | 0x20 | a space |
“\t” | 9 | 0x09 | a tab |
“\n” | 10 | 0x0A | a new line |
“\r” | 13 | 0x0D | a return |
“\0” | 0 | 0x00 | a NUL-byte |
“\v” | 11 | 0x0B | a vertical tab |
If you want to remove some specific characters, you can use the above as a second argument in the trim function.
Things, which we discuss on the PHP trim() function
- Remove spaces before and after from the string.
- PHP trim() function to remove specified characters.
- Use PHP trim with arrays.
- PHP trim the last character.
- Trim the comma from the end of the string.
Remove spaces before and after from the string using PHP trim()
1 2 3 4 5 6 7 |
<?php $str = ' PHP '; $new_str = trim($str); var_dump($new_str); ?> |
Output:
1 |
string(3) "PHP" |

Here we define a string with spaces at the start and end.
And then we use the trim function to remove whitespaces from both sides.
Also Read: 5 Most Useful PHP String Functions You Should Know
PHP trim() function to remove specified characters
When you run the below code, the trim function checks the character on both sides and removes the specified characters.
1 2 3 4 5 6 7 8 |
<?php // removes the predefined characters from // front and back $str = "Hello World!"; echo trim($str, "Hell!"); ?> |
Output:
o World

Use PHP trim With an Arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php function trim_value(&$value) { $value = trim($value); } $fruit = array('apple','banana ', ' cranberry '); var_dump($fruit); array_walk($fruit, 'trim_value'); var_dump($fruit); ?> |
In the above PHP trim array example, first, we create a function and take the array value as an argument.
Then we define an array of elements with some spaces.
Then we use the array_walk function to call the function with the array as an argument.
Output:

PHP trim last character of the String (rtrim)
PHP rtrim() function is used to remove characters from the right side of the string.
1 2 3 4 5 6 7 |
<?php $string = "Hello PHPCoder!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . rtrim($string, "!") . "\n"; ?> |
Output:
Original string: Hello PHPCoder!
Updated string: Hello PHPCoder

PHP trim comma from the end of the string
1 2 3 4 |
<?php $string = "phpcoder,"; echo $string = rtrim($string, ','); ?> |
Output:
phpcoder
Conclusion
This is the complete article and tutorial on the PHP trim() function with help of multiple examples. Here we can see the complete source code of each example and the types of trim functions.
I hope you all understand the complete source code.
If you are facing any issues please let me know in the comment below.
Happy Coding..!