Summary of usage of php string function trim()

怪我咯
Release: 2023-03-08 07:00:01
Original
2654 people have browsed it

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(&#39;<p></p>&#39;, &#39;</p>&#39;); // 输出为 <p
echo ltrim(&#39;www.php.cn&#39;,&#39;www.&#39;); // 输出为 php.cn
Copy after login

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]

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!