PHP trim() Function: Syntax, Examples

The trim() function is a built-in function in PHP which is used to remove white space or other specified characters from the beginning and end of the string. This function simply removes unwanted characters (like whitespace, tabs, newlines, etc.) from both ends of a string.

Syntax of PHP trim() Function


The general syntax to define trim() function in PHP is as follows:

trim(string $string, string $charlist): string

In the above syntax, there are two parameters named $string and $charlist.

  • $string is the input string you want to trim. This is a required parameter.
  • $charlist specifies the list of characters you want to remove from the beginning and end of the string. However, this is an optional parameter, which was added in PHP 4.1. If you don’t specify this, PHP will remove all whitespace characters by default.

Return Value

The trim() function returns the trimmed string with specified characters removed from both ends. In other words, this function will return a new string value after trimming.

Default Characters Removed by PHP trim() Function

If you don’t specify the second parameter, the trim() function automatically removes the following whitespace characters by default:

  • ” ” – space
  • “\n” – newline
  • “\r” – carriage return
  • “\t” – tab
  • “\v” – vertical tab
  • “\0” – NULL byte

Basic Examples of trim() Function in PHP


Example 1: Let’s write a PHP program in which we will remove the leading and trailing white spaces from the string using trim() function.

<?php
$text = " Hello, PHP! ";
$trimmedStr = trim($text);
echo $trimmedStr;
?>
Output:
      Hello, PHP!

Example 2: Let’s write a PHP program in which we will remove the custom character / from the beginning and end of a string using the trim() function.

<?php
$url = "/path/to/file/";
$trimmedUrl = trim($url, "/");
echo $trimmedUrl;
?>
Output:
      path/to/file

In this example, we have defined a string containing a forward slash / at both the start and end. Then, we have used the trim() function to remove the forward slash / from both ends of the string. We have passed the forward slash / as the second parameter to the trim() function. After trimming string, the output is: “path/to/file”.


In this tutorial, you learned about how to use the trim() function in PHP to remove whitespace or other specified characters from the beginning and end of the string. I hope you will have understood the basic syntax and practiced all examples.

Stay tuned with the next tutorial where you will learn about ltrim() and rtrim() functions in PHP which remove characters from the left and right end of a string, respectively.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.