Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the PHP trim() Function?
2.1.
Here is the basic syntax:
2.2.
Using the trim() Function
3.
Advanced Trimming with Character Mask
4.
Frequently Asked Questions
4.1.
What characters does trim() remove by default?
4.2.
Can trim() remove characters from the middle of the string?
4.3.
Can trim() work with variables?
5.
Conclusion
Last Updated: Sep 9, 2024
Easy

PHP trim() Function

Author Gunjan Batra
0 upvote

Introduction

When working with text data in PHP, you'll frequently find yourself needing to remove unwanted spaces from the start or end of a string. Thankfully, PHP provides us with a handy built-in function, trim(), designed to do exactly that.

PHP trim () function

In this blog, we will learn about PHP trim() function. 

What is the PHP trim() Function?

The trim() function in PHP is used to remove whitespace characters from the beginning and end of a string. Not only spaces, but it also removes other types of characters such as tabs (\t), newlines (\n), and carriage returns (\r).

Here is the basic syntax:

trim($string, $character_mask);

The $string parameter is required and represents the string to be trimmed. The $character_mask parameter is optional and allows you to specify which characters you want to remove from the string.

Using the trim() Function

Let's see a simple usage of trim() function:

$text = "   Hello, World!   ";
$trimmed = trim($text);
echo $trimmed;  // 

Outputs:

"Hello, World!"

In this example, the trim() function is used to remove the leading and trailing spaces from the string.

Advanced Trimming with Character Mask

PHP's trim() function also allows you to specify a set of characters to be removed from the string. This set of characters is referred to as the "character mask". If this parameter is not provided, PHP will remove the following characters.

" " (ASCII 32, space)

"\t" (ASCII 9, tab)

"\n" (ASCII 10, new line)

"\r" (ASCII 13, carriage return)

"\0" (ASCII 0, null)

"\x0B" (ASCII 11, vertical tab)

Here's an example of using the $character_mask parameter:

$text = "Hello, World!!!";
$trimmed = trim($text, "Hed!");

echo $trimmed;  // 

Outputs

"llo, World"

In this example, trim() removes 'H', 'e', 'd' and '!' from the start and end of the string.

Frequently Asked Questions

What characters does trim() remove by default?

By default, trim() removes whitespace, tab, newline, carriage return, null, and vertical tab.

Can trim() remove characters from the middle of the string?

No, trim() only removes characters from the start and end of the string.

Can trim() work with variables?

Yes, trim() can work with both string literals and string variables.

Conclusion

The PHP trim() function is a powerful utility for handling and manipulating strings. It is straightforward to use, flexible, and can help tidy up your text data. Understanding how to use trim() is an essential skill in your PHP toolkit. It enables you to create cleaner and more reliable data, enhancing the user experience and improving the overall quality of your PHP applications.

If you would like to learn more, check out our articles on

Refer to our guided paths on Code360 to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.
 

Live masterclass