Precautions for using trim functions in php-PHP source code

Source: Internet
Author: User
The trim function is used to delete cells, but we must pay attention to it when using it. Let's introduce some problems and bugs in the space deletion of the trim function. The trim function is used to delete cells, but we must pay attention to it when using it. Let's introduce some problems and bugs in the space deletion of the trim function.

Script ec (2); script


Trim-removes spaces (or other characters) at the beginning and end of the string)

Usage:

String trim (string $ str [, string $ charlist = "\ t \ n \ r \ 0 \ x0B"])

You should be familiar with the trim function. The optional charlist parameter is added from 4.1.0. The default usage is not too much. Let's talk about the use of the second parameter:

$ Path = trim (dirname (_ FILE _), '/'). '/'; // ensure that $ path has only one/end

There is no problem with this usage. Under what circumstances will the problem occur? Another example:

$ Domain = trim ('www .example.com.tw ', 'www.'); // The expected result is example.com.tw, but the actual result is example.com. t. w in tw is gone.

The problem occurs in the second parameter $ charlist, which represents a character list rather than a simple string. Therefore, w of tw belongs to www. A member in this list is removed together. For more information, see trim.

Alternative method:

$ Domain = preg_replace ('/^ www \. | www \. $/', ", 'www .example.com.tw ');

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.