PHP string function trim () Usage Summary

Source: Internet
Author: User
The string function trim () is to remove white space characters or other predefined characters on either side of the string, which is very important in our program development, this article on the mobile phone a few about the PHP string function trim () Usage Summary, I hope you understand the trim () function to help, come together to see it.

1. Remove the PHP function trim () for the string leading and trailing spaces and special characters

When you usually let the user form or other text box input data, it is possible to accidentally enter a space or other special characters and so on. In some cases, spaces and special characters are not allowed in the string, and some things need to be used to remove these spaces and special characters. The trim () function is provided in PHP to remove whitespace and special characters from the left and right sides of the string, the LTrim () function removes the space and special characters from the left of the string, the RTrim () function removes the space and special characters to the side of the string. Using these PHP self-bringing functions can be very convenient to do these operations. The syntax and usage of these functions are described below.

2.PHP function RTrim () The strange phenomenon in use

A strange problem was encountered with the RTrim () function today:

echo RTrim (' <p></p> ', ' </p> '); The output is <pecho ltrim (' www.php.cn ', ' www. '); Output is php.cn

The above output is a bit surprising, originally I think the first line should output <p>, and the second row output jb51.net.

This problem has been tangled up in me for a long time, has not found the reason, later in the handbook found the answer:

RTrim () is replaced with a character, not as a string. </p>6 characters will be replaced from right to left, and then to the left, because > is also contained in the string (</p>) of the second argument of Rtirm (), so it is also replaced, and when it goes to the left, it encounters P, P is not included in the string of the second argument. So replace stop, output <p.

3.PHP trim () function easy to use guide

The PHP function trim () removes white-space characters and other predefined characters from both ends of the string, with the specific syntax trim (str,charlist), which we'll explain in code examples; trim (str,charlist) parameter 1 str is the string to be manipulated, parameter 2 charlist Optional, specifies the special symbol you want to remove.

4.PHP LTrim and RTrim remove left and right spaces and special character instances

This article mainly introduces the LTrim and RTrim in PHP to remove the use of left and right spaces and special characters, combined with examples of the form of a more detailed description of the definition of LTrim and RTrim, parameters and remove the right and left spaces and special characters of the use of skills, the need for friends can refer to the next, This paper analyzes the use of LTrim and RTrim in PHP to remove left and right spaces and special characters, as follows:

"The relevant question and answer recommendation"

Php-trim function for full-width whitespace processing

Error using trim () function under Javascript-ie Browser

"Recommended for related terms"

Trim

RTrim

LTrim

"Related articles recommended"

The difference between PHP trim function LTrim function RTrim function

PHP Common String Operation function Example summary trim, NL2BR, addcslashes, UUDecode, MD5, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.