Menu

PHP trim(): Remove Whitespace from a String

trim($string) removes spaces, tabs and newlines from both ends of a string; ltrim() and rtrim() clean one side. Learn how to trim specific characters, the full-width Japanese space, and every value in an array.

This page includes runnable editors - edit, run, and see output instantly.

trim($string) returns the string with whitespace removed from the start and the end: spaces, tabs, newlines, carriage returns, NUL bytes and vertical tabs. ltrim() removes it only from the start and rtrim() only from the end. Spaces in the middle are kept.

The var_dump() output shows the length of each result, which makes the removed characters visible. Trimming is the first thing to do with most values from a form (passwords are the exception, see the end of the page): a user who types ada@example.com with a trailing space should still be able to log in.

Syntax

trim(string $string, string $characters = " \n\r\t\v\0"): string
ltrim(string $string, string $characters = " \n\r\t\v\0"): string
rtrim(string $string, string $characters = " \n\r\t\v\0"): string

trim() returns a new string; it does not change the variable you pass in.

Trim specific characters

The second argument replaces the default list. Every character in it is removed from the ends, in any order and any number of times. .. defines a range, so 'a..z' means every lowercase letter.

To also keep removing normal whitespace, include it in the list: trim($s, " \n\r\t\v\0/").

The second argument is a set of characters, not a string

trim() does not remove a word or suffix. It removes any of the characters you list, from both ends, until it reaches one that is not in the list. That surprises people who try to strip a file extension:

The first line removes the h at the start too, because h is in the set. To remove a fixed prefix or suffix, use str_starts_with() or str_ends_with() with substr(), or str_replace() if the text cannot appear anywhere else.

Trim the full-width space (Japanese input)

Japanese keyboards often produce the ideographic space   (U+3000), for example around a name typed into a form. It is not in trim's default list, and listing it in the second argument is a trap: trim() works byte by byte, the space is the 3 bytes E3 80 80, and E3 is also the first byte of every hiragana and katakana character, so a name that starts with one loses that character's first byte. Use a regular expression with the u modifier:

\A and \z anchor to the start and end of the string. With the u modifier, \s matches every Unicode space: the ASCII whitespace, the full-width space and the non-breaking space (U+00A0) that copy and paste from web pages often brings. Without u, \s matches ASCII whitespace only and the full-width space stays. PHP 8.4 adds mb_trim(), mb_ltrim() and mb_rtrim() for this; on PHP 8.3 and earlier the regex is the way.

Trim every value in an array

array_map('trim', $array) trims each value and keeps the keys. A typical case is a comma separated list from a text field, split with explode():

Trim a form value before validating it

Trim first, then check for an empty string. A field that contains only spaces is then rejected as empty. Run this block, type a name with spaces around it into the form and press Send: the same script runs again with $_POST filled in.

Do not trim passwords: a space at the end of a password is part of it.

Frequently Asked Questions

What does trim() remove in PHP?

By default, from both ends of the string: spaces, tabs (\t), newlines (\n), carriage returns (\r), NUL bytes (\0) and vertical tabs (\v). It never touches the middle of the string.

What is the difference between trim, ltrim and rtrim?

trim() cleans both ends, ltrim() only the start and rtrim() only the end. All three take the same optional list of characters to remove.

Why does trim() not remove the Japanese full-width space?

The full-width space (U+3000) is not in trim's default list and is 3 bytes in UTF-8. Use preg_replace('/\A\s+|\s+\z/u', '', $s): with the u modifier, \s matches every Unicode space, U+3000 included. PHP 8.4 adds mb_trim(), which removes it too.

How do I remove the last comma from a string in PHP?

rtrim($s, ',') removes every trailing comma. If a space may follow it, include the space too: rtrim($s, ', ').

How do I trim every value in an array?

array_map('trim', $array) returns a new array with every value trimmed, keeping the keys. For nested arrays, use array_walk_recursive() with a reference.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED