trim()
The trim() function in PHP is used to remove whitespace or other predefined characters from both the beginning and end of a string. In this article, we will
Introduction
The trim() function in PHP removes whitespace (or other characters you specify) from both the start and the end of a string. It is one of the most common string functions you will reach for, because text from forms, files, and HTTP requests almost always arrives with stray spaces, tabs, or newlines around it.
This chapter covers the syntax, the default characters trim() strips, how to remove your own custom character set, how trim() differs from ltrim() and rtrim(), and the most common mistakes to avoid.
Syntax
trim(string $string, string $characters = " \n\r\t\v\x00"): string| Parameter | Description |
|---|---|
$string | The input string to trim. |
$characters | Optional. The set of characters to strip. When omitted, the default whitespace set is used. |
The function returns a new, trimmed string — it does not modify the original $string (strings in PHP are passed by value).
By default trim() removes these characters from both ends:
| Character | Meaning |
|---|---|
" " | An ordinary space |
"\t" | A tab |
"\n" | A line feed (new line) |
"\r" | A carriage return |
"\0" | The NUL byte |
"\x0B" (\v) | A vertical tab |
Basic example
Strip the leading and trailing spaces from a string:
The square brackets are printed only to make the result visible — they prove the spaces on both sides are gone while the inner space (Hello, world!) is kept.
Removing custom characters
The second argument lets you choose exactly which characters to strip. This is handy for cleaning up delimiters, slashes, or quotes:
<?php
echo trim("xxxHello worldxxx", "x"), "\n"; // Hello world
echo trim("/var/www/html/", "/"), "\n"; // var/www/html
echo trim("###Section###", "#"), "\n"; // SectionYou can pass several characters at once — trim() removes any of them from each end until it hits a character that is not in the set:
<?php
echo trim("**[Note]**", "*[]"); // NoteNote that only the ends are affected: the brackets inside the string are untouched, just like the inner space in the first example.
Character ranges with ".."
Inside the $characters argument you can specify a range with two dots. For example, to strip ASCII control characters:
<?php
$dirty = "\x01\x02Hello\x03";
echo trim($dirty, "\x00..\x1F"); // Hellotrim() vs ltrim() vs rtrim()
trim() works on both ends. When you only want one side, use its siblings:
| Function | Trims from |
|---|---|
trim() | both ends |
ltrim() | the left (start) only |
rtrim() | the right (end) only |
<?php
$value = "...data...";
echo trim($value, "."), "\n"; // data
echo ltrim($value, "."), "\n"; // data...
echo rtrim($value, "."), "\n"; // ...dataCommon use case: cleaning user input
trim() is most valuable when validating form input. A user might type a trailing space after their email or leave the field blank with only spaces. Trimming first makes those checks reliable:
<?php
$username = " alice ";
$username = trim($username);
if ($username === "") {
echo "Username is required.";
} else {
echo "Welcome, " . $username . "!"; // Welcome, alice!
}Without the trim() call, a value of " " (only spaces) would pass an if (!empty($username)) check even though it is effectively empty.
Gotchas
- It returns a value; it does not mutate.
trim($string);on its own does nothing useful — you must assign the result:$string = trim($string);. - The custom set is a list, not a substring.
trim("Hello", "Helo")strips every leading/trailing character that appears in"Helo", not the literal word — so it can eat more than you expect. - Multibyte characters.
trim()operates on bytes, not Unicode code points. To strip multibyte whitespace, use a custom set or a regular expression instead. - It only touches the ends. To remove characters everywhere, reach for
str_replace()orpreg_replace()instead.
Conclusion
The trim() function is the go-to tool for cleaning the edges of a string — whitespace by default, or any character set you provide. Combined with its one-sided siblings ltrim() and rtrim(), it makes input validation and string normalization simple and predictable.
To keep learning about PHP string handling, see PHP Strings, str_replace(), explode(), and str_pad().