The string function trim() removes blank characters or other predefined characters on both sides of the string. This is very important in our program development. This article has published several articles about the PHP string function trim(). ) usage summary, I hope it will be helpful for everyone to understand the trim() function, let’s take a look.
When users usually enter data in a form or other text box, a You may accidentally enter spaces or other special characters, etc. In some cases, spaces and special characters are not allowed in the string. In this case, you need to use something to remove these spaces and special characters. PHP provides the trim() function to remove spaces and special characters on the left and right sides of a string, the ltrim() function to remove spaces and special characters on the left side of a string, and the rtrim() function to remove spaces and special characters on the right side of a string. These operations can be easily performed using these PHP built-in functions. The following introduces the syntax and usage of these functions.
2.Weird phenomena in the use of PHP function rtrim()
I encountered a strange problem when using the rtrim() function today:
echo rtrim('<p></p>', '</p>'); // 输出为 <p echo ltrim('m.sbmmt.com','www.'); // 输出为 php.cn
The above output is a bit unexpected. Originally, I thought the first line should output
, and the second line would output jb51.net.
This problem has troubled me for a long time, and I have never found the reason. Later, I found the answer in the manual:
rtrim() replaces characters in units, not strings. When replacing from right to left, the 6 characters of
will definitely be replaced. When going to the left, > is encountered, because > is also included in the string of the second parameter of rtirm() (< /p>), so it is also replaced. When going further to the left, p is encountered. At this time, p is not included in the string of the second parameter. So the substitution stops and
3.A simple guide to using the trim() function in PHP
The PHP function trim() removes blank characters and other predefined characters from both ends of a string. The specific syntax is trim(str, charlist), we will explain it in detail with code examples; trim(str, charlist) parameter 1 str is the string to be operated, parameter 2 charlist is optional, and specifies the special characters you want to remove. symbol.
4.Examples of removing left and right spaces and special characters with ltrim and rtrim in PHP
This article mainly introduces the removal of left and right spaces and special characters with ltrim and rtrim in PHP Usage, combined with examples, explains in detail the definitions of ltrim and rtrim, the role of parameters and the use skills of removing left and right spaces and special characters. Friends in need can refer to it. This article analyzes the removal of left and right spaces between ltrim and rtrim in PHP with examples The usage of spaces and special characters is as follows:
[Related Q&A recommendations]
php - trim function's processing of full-width spaces
javascript - Error when using trim() function under IE browser
[Recommended related entries]
【Recommended related articles】
php trim function ltrim function rtrim function difference
The above is the detailed content of Summary of usage of php string function trim(). For more information, please follow other related articles on the PHP Chinese website!