The string function trim () is used to remove white spaces or other predefined characters on both sides of the string, which is very important in our program development, this article provides a summary of the usage of the php string function trim (). I hope it will help you understand the trim () function. let's take a look. The string function trim () is used to remove white spaces or other predefined characters on both sides of the string, which is very important in our program development, this article provides a summary of the usage of the php string function trim (). I hope it will help you understand the trim () function. let's take a look.
1. PHP function trim () for removing spaces at the beginning and end of a string and special characters ()
When entering data in user forms or other text boxes, you may accidentally enter spaces or other special characters. In some cases, spaces and special characters are not allowed in strings. in this case, you need to remove these spaces and special characters. The trim () function is provided in PHP to remove spaces and special characters on both sides of the string. the ltrim () function removes spaces and special characters on the left side of the string, rtrim () the function removes spaces and special characters from the right side of the string. Using these PHP built-in functions, you can easily perform these operations. The following describes the syntax and usage of these functions.
2. weird usage of PHP function rtrim ()
Today, when I used the rtrim () function, I encountered a strange problem:
Echo rtrim ('',''); // The output is
The output result is a little unexpected. I thought the first line should be output.
The second line outputs jb51.net.
I have been entangled in this problem for a long time, but I have not found the cause. later I found the answer in the manual:
Rtrim () is replaced by characters rather than strings. Replacement from right to left
The six characters will surely be replaced, and> will be encountered when going to the left, because> is also included in the string of the second parameter of rtirm (
), So it is replaced. when p is encountered again to the left, p is not included in the string of the second parameter. So the replacement is stopped, and the output is
< p>
3. Guide to simple use of trim () functions in PHP
The PHP function trim () deletes white spaces and other pre-defined characters from both ends of the string. The syntax is trim (str, charlist). We will explain it in detail using code examples; trim (str, charlist) parameter 1 str is the string to be operated, parameter 2 charlist (optional) specifies the special symbol to be removed.
4. in PHP, ltrim and rtrim are used to remove left and right spaces and special characters.
This article mainly introduces the use of ltrim and rtrim in PHP to remove left and right spaces and special characters. The definition of ltrim and rtrim is described in detail in the form of examples, the function of parameters and tips for removing left and right spaces and special characters. For more information, see. This article provides an example of how to use ltrim and rtrim in PHP to remove left and right spaces and special characters, the details are as follows:
[Related Q & A recommendations]
Php-trim function processing of full-width spaces
Trim () function error in javascript-IE browser
[Related term recommendation]
Trim
Rtrim
Ltrim
[Recommended related articles]
Differences between the php trim function and the ltrim function rtrim function
Summary of common PHP string operation function instances: trim, nl2br, addcslashes, uudecode, md5, etc.
The above is a summary of php string function trim () usage. For more information, see other related articles in the first PHP community!
<>