PHP Trim Function: Remove Spaces from Strings

The trim function is used to remove leading and trailing spaces or other predefined characters from a string. The syntax of the function is:

trim(string, characters)

In this case, the string refers to the string being processed, and characters refer to the characters to be removed (defaulting to spaces, tabs, and line breaks).

For example, use the trim function to remove leading and trailing whitespaces from a string.

$str = "  Hello, World!  ";
echo trim($str); // 输出:Hello, World!
bannerAds