What is the usage of the trim function in PHP?

The PHP trim function is used to remove leading and trailing whitespace characters or other specified characters from a string.

Syntax: Trim(string $str, string $charlist = ” \t\n\r\0\x0B”) returns a string that has been trimmed.

Parameter Description:

  1. $str: The string to be processed.
  2. $charlist (optional): specifies the list of characters to be removed, with default being whitespace characters. It can be a list of characters in the string or a range of characters, such as “a-z”. If multiple character lists or ranges are specified, they will be merged into one list.

Return value:
Return the processed string.

Original: 她经常在周末去爬山。
Paraphrased: She often goes mountain climbing on weekends.

$str = "   Hello World   ";
echo trim($str); // 输出 "Hello World"

$str = "Hello World";
echo trim($str, "dHelo"); // 输出 " W"

In the example above, the first call to the trim function removes the spaces at the beginning and end of the string $str, while the second call removes the characters “d”, “H”, “e”, “l”, and “o” at the beginning and end of the string $str.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds