trim() / ltrim() / rtrim() Since: PHP 4(2000)
Removes whitespace characters or specified characters from the beginning and/or end of a string. Use ltrim() or rtrim() to remove characters from only one side.
Syntax
// Removes whitespace from both the beginning and end. trim($string, $characters); // Removes whitespace from the beginning only. ltrim($string, $characters); // Removes whitespace from the end only. rtrim($string, $characters);
Functions
| Function | Description |
|---|---|
| trim($string, $characters) | Returns a new string with whitespace removed from both the beginning and end. If a second argument is provided, those characters are removed instead of whitespace. |
| ltrim($string, $characters) | Returns a new string with whitespace removed from the beginning only. |
| rtrim($string, $characters) | Returns a new string with whitespace removed from the end only. |
Return Value
Returns a new string with the whitespace or specified characters removed. The original string is not modified.
Sample Code
<?php
$str = " Hello World ";
// Removes whitespace from both the beginning and end.
echo trim($str); // Outputs 'Hello World'.
// Removes whitespace from the beginning only.
echo ltrim($str); // Outputs 'Hello World '.
// Removes whitespace from the end only.
echo rtrim($str); // Outputs ' Hello World'.
// Whitespace in the middle of the string is left unchanged.
echo strlen(trim($str)); // Outputs '11'.
// The second argument specifies which characters to remove.
echo trim("/path/to/file/", "/"); // Outputs 'path/to/file'.
echo ltrim("###Heading", "#"); // Outputs 'Heading'.
// Multiple characters can be specified for removal.
echo trim("===Title===", "="); // Outputs 'Title'.
// Tabs and newlines are also treated as whitespace and removed.
$code = "\t\n Result \n\t";
echo trim($code); // Outputs 'Result'.
// Example: validating form input.
$input = " ";
if (trim($input) === "") {
echo "This field is empty."; // Detects input containing only whitespace.
}
// Example: removing surrounding whitespace from a CSV value.
$csv_value = " New York ";
echo trim($csv_value); // Outputs 'New York'.
Overview
trim() removes whitespace characters from the beginning and end of a string. In addition to regular spaces, it also removes tabs, newlines, carriage returns, null bytes, and vertical tabs. Whitespace in the middle of the string is not affected.
If you pass a string as the second argument, those characters are removed instead of whitespace. This is useful for stripping slashes from file paths or removing Markdown symbols. Each character in the second argument is treated as an individual removal target, so trim($str, "/\\") removes both forward slashes and backslashes.
When validating form input, trim() is essential for removing accidental leading/trailing spaces entered by the user and for detecting input that contains only whitespace. For replacing characters within a string, use str_replace(). For extracting substrings, use substr() / mb_substr().
If you find any errors or copyright issues, please contact us.