Home>Article>Backend Development> Summary of usage of php string function trim()

Summary of usage of php string function trim()

怪我咯
怪我咯 Original
2017-06-09 14:05:00 2650browse

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.

1.The PHP function trim() that removes spaces and special characters from the beginning and end of a string

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('

', '

'); // 输出为

The above output is a bit unexpected. Originally, I thought the first line should output e388a4556c0f65e1904146cc1a846bee, 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 94b3e26ee717c64999d7867364b1b4a3 will definitely be replaced. When going to the left, > is encountered, because > is also included in the string of the second parameter of rtirm() (6fb279ad3fd4344cbdd93aac6ad173ac), 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]

trim

rtrim

ltrim

【Recommended related articles】

php trim function ltrim function rtrim function difference

Summary of PHP common string manipulation function examples trim, nl2br, addcslashes, uudecode, md5, etc.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn