Trim in PHP is a built-in PHP function that eliminates white spaces as well as pre-defined characters from both the left and right sides of a string.

Syntax

trim(string,charlist)

As demonstrated in the above syntax and discussed below, the function accepts one obligatory parameter and one optional parameter.

  • $string: This parameter defines the string from which the left and right white space and pre-defined characters will be deleted.
  • $charlist: This is an optional parameter that defines which characters from the string should be deleted. It eliminates all the following characters if this is not specified. 

Return Value: It returns a string that was edited by deleting white space and pre-defined characters on both sides.

As a result, if you do not specify the second parameter, the Trim in PHP function trims the following parameters by default:

  • The character ” ” (ASCII 32 (0x20) is a standard space character
  • The tab character from Trim in PHP is “t” (ASCII 9 (0x09), and the new line character is “n” (ASCII 10 (0x0A)
  • The carriage return character is “r” (ASCII 13 (0x0D)
  • “0” (ASCII 0 (0x00) represents the NULL byte)
  • The character “x0B” (ASCII 11 (0x0B) signifies a vertical tab

Stand Out From Your Peers this Appraisal Season

Start Learning With Our FREE CoursesEnroll Now
Stand Out From Your Peers this Appraisal Season

Example: 

<?php

// PHP program to demonstrate the use of Trim in PHP

// function when second parameter is present

// removes the predefined characters from

// front and back

$str = "Hello World!";

echo trim($str, "Hell!");

?>

Output

Trim_IN_PHP_1

Example

<?php

// PHP program to demonstrate the use of Trim in PHP

// function when second parameter is absent

$str = " I love Frenh ";

// removes all leading and trailing whitespaces

echo trim($str);

?>

Output

Trim_IN_PHP_2.

What Are the Three Different Types of Trim Functions?

Trim() removes characters from the beginning and end of a string, thus when you remove characters from the center, it can be misleading. trim('abc', 'bad') eliminates both 'a' and 'b' by trimming 'a' and then relocates 'b' to the beginning to be trimmed as well. As a result, trim ('abc', 'b') appears to "work" whereas trim ('abc', 'b') appears to "fail." Trim in PHP comes in three flavors, you can use each of which for the purposes listed below:

  • trim(): This is a standard function that is used to remove characters from the beginning and end of a string.
  • ltrim() is a function that trims characters from the start of an input string.
  • rtrim() is a function that trims characters off the end of a string.

Example

<?php

$str = "\n\nDemo text!\n\n";

echo "Trim: " . ($str);

?>

Output

Trim_IN_PHP_3

This example demonstrates the results without using the Trim in PHP function in the previous example. It displays newlines as part of the sentence in the string assigned for testing purposes. In the output, you can see the same thing.

To Remove a Character

<?php

$str1=" Just an example";

$str_new=trim($str1,"\t");

//echo $str_new;

var_dump($str_new);

?>

Output

Trim_IN_PHP_4.

In the preceding example, you use the trim function to delete the tab (“t”) character by specifically stating the character in the trim function's second parameter, as shown. First, you must assign the required string to the str1 argument, and then use the $str new parameter to append a tab to the end of the $str1. Simultaneously, you invoke the trim function, removing the tab character from the string's end.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

When you remove the trim method from the code, you can see that an extra tab is written at the end of the string because it was appended in a $str new parameter. As a result, the Trim in PHP functionality has been confirmed.

To Remove a Blank Space

Example

<?php

$str = "

No blank space

";

// Displays "No blank space"

echo trim( $str );

?>

Output

Trim_IN_PHP_5.

The input string in the preceding example has a lot of blank spaces at the beginning. As a result, you’re going to use the trim function to get rid of it. As can be seen in the result, all of the blank spaces from the beginning have been removed, leaving only the correct input string. Please keep in mind that only the whitespace between the start and finish of the string is cut, not the ones in the middle of the sentence. The spaces between "example" and "to" are still the same.

Example

<?php

$text   = "\t\tThese are a few words :) ...  ";

$binary = "\x09Example string\x0A";

$hello  = "Hello World";

var_dump($text, $binary, $hello);

print "\n";

$trimmed = trim($text);

var_dump($trimmed);

$trimmed = trim($text, " \t.");

var_dump($trimmed);

$trimmed = trim($hello, "Hdle");

var_dump($trimmed);

$trimmed = trim($hello, 'HdWr');

var_dump($trimmed);

// trim the ASCII control characters at the beginning and end of $binary

// (from 0 to 31 inclusive)

$clean = trim($binary, "\x00..\x1F");

var_dump($clean);

?>

Output

Trim_IN_PHP_6

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion 

In this article, you went through the trim(), ltrim(), and rtrim() functions in detail, which are used to eliminate characters from a string that aren't required. You also learned how to trim using your whitespace character. If you want to learn more about the Trim in PHP concept, then you must enroll in this Simplilearn's Full Stack Development course. Also if you are looking for a professional degree in the field then you must look for our skill up course and enjoy the best knowledge. 

Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, ASAP.

Happy learning!