How to Use PHP trim() to Remove Whitespace and Specified Characters

This article explains the PHP trim() function, detailing its default behavior of removing whitespace from both ends of a string, how to use the optional character mask to strip specific characters, and provides clear code examples with expected output for each case.

php Courses
php Courses
php Courses
How to Use PHP trim() to Remove Whitespace and Specified Characters

In PHP development you often need to manipulate strings, and the trim() function is a common tool for removing whitespace from both ends of a string.

The basic syntax of the function is:

string trim(string $str, string $character_mask = " ")

The function accepts two parameters: the target string $str and an optional character mask $character_mask. By default, $character_mask removes spaces, tabs, newlines, and other whitespace characters.

Below is a simple example demonstrating the default usage of trim():

$str = "  Hello, World!   ";
echo "Original string: '" . $str . "'";
echo "Trimmed string: '" . trim($str) . "'";

Output:

Original string: '  Hello, World!   '
Trimmed string: 'Hello, World!'

In this example the original string contains extra spaces at both ends, which are removed by trim($str), leaving a clean string.

Beyond the default behavior, trim() can also remove specific characters by providing a $character_mask argument.

Example with a character mask:

$str = "XOXHello, World!OXOX";
echo "Original string: '" . $str . "'";
echo "Trimmed string: '" . trim($str, "XO") . "'";

Output:

Original string: 'XOXHello, World!OXOX'
Trimmed string: 'Hello, World!'

Here the original string has leading and trailing "X" and "O" characters; using trim($str, "XO") removes those characters from both ends.

Note that trim() only removes characters from the start and end of a string; characters in the middle are left untouched.

Summary

Through this article you learned the basic usage of the PHP trim() function, which can easily remove whitespace or specified characters from the ends of a string, making string handling more efficient in everyday PHP development.

Java learning materials

C language learning materials

Frontend learning materials

C++ learning materials

PHP learning materials

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendStringTutorialfunction.trim
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.