PHP trim() Function
PHP trim() Syntax
trim(string $string, string $characters = " \n\r\t\v\x00"): string
PHP trim() Basic examples
$text = " Hello world ";
echo trim($text);
Hello world
Removes leading and trailing whitespace from a string.
$text = "\n\tHello\t\n";
echo trim($text);
Hello
Removes newlines and tab characters by default.
$text = "***Hello***";
echo trim($text, "*");
Hello
Removes custom characters from both ends of the string.
PHP trim() Real-world usage
$input = " user@example.com ";
$email = trim($input);
echo $email;
user@example.com
Cleans user input before validation or storage.
$csvValue = " \"value\" ";
echo trim($csvValue, " \"");
value
Strips spaces and quotes when parsing CSV-like data.
PHP trim() Edge cases
$text = "";
echo trim($text);
Trimming an empty string returns an empty string.
$text = " ";
echo trim($text);
A string containing only whitespace becomes an empty string.
PHP trim() Common mistakes
Assuming trim removes whitespace in the middle
trim() only affects the beginning and end of a string.
echo trim("hello world");
echo preg_replace('/\s+/', ' ', trim("hello world"));
Use additional logic if you need to normalize internal whitespace.
Forgetting custom character masks
trim() removes only whitespace unless a character mask is provided.
echo trim("***value***");
echo trim("***value***", "*");
Provide a second parameter to remove specific characters.
PHP trim() Frequently Asked Questions
What does PHP trim() do?
The trim() function removes whitespace or specified characters from the beginning and end of a string.
Does trim() remove spaces in the middle of a string?
No, trim() only removes characters from the start and end of a string. To remove spaces inside the string, use preg_replace().
How to remove all whitespace in PHP?
Use preg_replace('/\s+/', '', $string) to remove all whitespace characters from a string.
What characters does trim() remove by default?
By default, trim() removes spaces, tabs, newlines, carriage returns, vertical tabs, and null bytes.
How to trim specific characters in PHP?
Pass a second parameter to trim(), for example trim($string, '*') to remove asterisks.
What is the difference between trim(), ltrim(), and rtrim()?
trim() removes characters from both ends, ltrim() removes from the left, and rtrim() removes from the right.
Does trim() modify the original string?
No, trim() returns a new string and does not modify the original variable unless reassigned.
Is trim() safe for user input?
Yes, trim() is commonly used to clean user input, but it should be combined with validation and sanitization.
What happens if you trim an empty string?
Trimming an empty string returns an empty string.
Can trim() remove numbers or letters?
Yes, if specified in the character mask, trim() can remove any characters including numbers and letters.
How to remove newlines from a string in PHP?
trim() removes newlines at the start and end, but for all newlines use str_replace() or preg_replace().
Does trim() remove Unicode whitespace?
No, trim() only removes ASCII whitespace by default. Use regex for Unicode support.