PHP Trim Function With Examples

What is PHP trim() Function?

The trim() function is a built-in function of PHP with the primary purpose of removing the whitespaces and excess characters from both sides of the string.

From the name it suggests, the trim() function is able to trim any string values on its right and left sides.

To see how it works let us first know the following information.

What does the PHP trim() function do?

The PHP trim() function does the trimming of whitespace and excess characters of a string.

While the trim() function trims characters on both sides of the string, you can also use the following functions related to trim():

  • ltrim()
  • rtrim()

Both ltrim() and rtrim() functions are related to the PHP trim() function.

The only difference is that the ltrim() can trim characters only on the left side of the string and rtrim() can do the same on the right side of the string.

Therefore, if you only need to trim one side of your string, then you can either use the ltrim() or the rtrim().

Now, let us know the following characteristics of the PHP trim() function to know more about it.

Additionally, you have to note that the following also applies to ltrim() and rtrim() functions.

Syntax

trim( $string, $character_list)

Parameter Values

As you can see in the syntax of the function, it takes two parameters which are the $string and the $character_list.

String

The string argument is a required parameter of the trim() function. It specifies the string that the function should trim and it serves as the basis of the program.

Character List

The character list, on the other hand, is an optional parameter. This parameter specifies the characters that the function should omit from the string.

To specify the characters that you want to trim from the string, you can list all the characters in the character list parameter. Additionally, you can also specify the number of characters to omit using the “...” declaration.

Return Values

The PHP trim() function returns a string with the omitted whitespace from the left and right sides of the string.

However, if the second parameter does not specify the characters to trim, the function will strip the following:

  • NULL (\0)
  • Tab (\t)
  • The new line (\n)
  • Vertical tab (\x0B)
  • Carriage return (\r)
  • Ordinary white space (" ")

trim() function in PHP Example

In this section, we will now have an example program that which we can implement the PHP trim() function and see how it works.

Example 1:

<?php
$sample = "***Good day!//";
echo $sample . "<br>";
echo trim($sample,"*/");
?>

For example 1, we simply use the trim() function to trim the characters that we want to omit.

To be specific, we include in the parameter the characters that we want to remove from the string.

As a result, the output displays the string with the removed specified characters.

Output:

***Good day!//
Good day!

Example 2:

<?php
$sample = "***Good day!//";
echo $sample . "<br>";
// implementing ltrim() function
echo "Using left trim function: " . ltrim($sample,"*...") . "<br>";
// implementing rtrim() function
echo "Using right trim function: " . rtrim($sample,".../");
?>

In example 2, we also use the ltrim() and the rtrim() function to see how would they express their function.

Output:

***Good day!//
Using left trim function: Good day!//
Using right trim function: ***Good day!

As the program’s output shows, both rtrim() and ltrim() functions correspond to the function that a user expects.

The rtrim() removed the characters on the right side of the string and it goes the same with the ltrim but on the left side.

Conclusion

Wrapping up, the topic has covered the essential discussions that are necessary to understand the PHP trim() function.

Also, we provide the related functions to the trim which were the ltrim() and the rtrim() functions.

As a recap, the trim() function basically removes the characters on both sides of the string.

Characters on the left side only of the string, you can use the ltrim() function.

But if you want to remove characters on the right side of the string, you may apply the rtrim() function.

If you want to discuss more topics related to this, you can have your suggestions written in the comments below. Have a good day!

Leave a Comment