PHP trim() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, trim() removes whitespace or other characters from the start and end of a string.

Use it to clean user input before you save or compare it.

Syntax

Call trim() with the values you need. Start with this line.

PHP
$clean = trim($input);
CallWhat it does
trim(" hello ")Returns "hello".
trim("--price--", "-")Returns "price".
trim("abcHELLOcba", "a..c")Uses a range. Returns "HELLO".

By default trim() removes spaces, tabs, new lines, carriage returns, vertical tabs, and NUL bytes. See ltrim() and rtrim() to clean only one side.

Library declarations
PHP
trim(string $string, string $characters = " \n\r\t\v\x00"): string

Parameters

ParameterDescription
stringRequired. The string to clean.
charactersOptional. The characters to remove. A range such as "a..z" is allowed. Default is whitespace.

Return Value

Return ValueDescription
stringThe string with the characters removed from both ends.

PHP Examples

Example 1 — Remove Extra Spaces

The most common use: clean spaces around a value.

PHP
<?php
$name = "   Asha   ";

echo "[" . $name . "]", PHP_EOL;
echo "[" . trim($name) . "]", PHP_EOL;

How It Works

  1. Original value. It has spaces on both sides.
  2. Print with brackets. Brackets make the spaces easy to see.
  3. Call trim(). It removes the edge spaces.
  4. Result. You get [Asha].

Example 2 — Custom Characters and Ranges

Pass a second argument to trim any characters you like.

PHP
<?php
echo trim("--price--", "-"), PHP_EOL;
echo trim("abcHELLOcba", "a..c"), PHP_EOL;

How It Works

  1. One character. "-" is removed from both ends.
  2. A range. "a..c" means a, b, and c.
  3. Edges only. Letters inside HELLO are never touched.
  4. Default is replaced. When you set characters, spaces are no longer trimmed unless you list them.

Example 3 — Clean Form Input

Remove hidden new lines and spaces from submitted text.

PHP
<?php
$input = "  [email protected] \n";
$clean = trim($input);

echo strlen($input), " -> ", strlen($clean), PHP_EOL;
echo $clean, PHP_EOL;

How It Works

  1. Dirty input. It has two spaces in front and a space and new line at the end.
  2. Trim it. All four edge characters are removed.
  3. Compare lengths. The length drops from 20 to 16.
  4. Safe to use. The clean email can now be compared or stored.

Frequently Asked Questions

trim() removes whitespace, or characters you specify, from the beginning and end of a string.
string trim(string $string, string $characters = " \n\r\t\v\x00"). Only the string is required.
No. It only works on the start and end. Use str_replace() or preg_replace() to change the middle.
Use ltrim() for the left side and rtrim() for the right side. rtrim() is also called chop().
It is a list of characters, not a whole word. Use ".." for a range, for example "0..9" removes any digit from the edges.

Next: ucfirst()

ucfirst() makes the first character of a string uppercase.

ucfirst() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful