Considerations for using Trim functions in PHP

Source: Internet
Author: User


trim-to remove whitespace characters (or other characters) at the end of a string

Usage:

String Trim (String $str [, String $charlist = "\t\n\r\0\x0b"])

Trim function Everyone should not be unfamiliar with the new optional charlist parameters from 4.1.0. The default usage is not much to say, say in the case of using the second parameter:

$path = Trim (dirname (__file__), '/'). ' /’; Guaranteed $path only one/end

There is no problem with this usage. What's going to go wrong? Another example:

$domain = Trim (' www.example.com.tw ', ' www. '); The expected result is example.com.tw, but the actual result is the EXAMPLE.COM.T,TW in the W.

The problem appears in the second parameter $charlist, which represents a list of characters rather than a simple string, so TW's w belongs to www. One of the members of this list was removed together. You can refer to the trim again

Alternate method:

$domain = Preg_replace ('/^www\.| www\.$/', ', ' www.example.com.tw ');

Related Article

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.