PHP 2 ways to turn the trailing blanks

Source: Internet
Author: User
Tags vars

Seemingly simple question, actually still a bit of pit, first here   space escape, not the string, directly with trim () is not to go off.

1, replace with Preg_replace

View copy print?
    1. $test = " dfadad  on the 775fd   of the relationship between the Sanying and the responsible people";
    2. $test = preg_replace ('/^ ( |\s) *| (   |\s) *$/', ', $test);
    3. Var_dump ($test);
    4. The results are as follows:
    5. String ' dfadad  on the relationship between the responsible people and Sanying 775FD ' (length=35)

This method is provided by the small partners inside the group, which expresses thanks. Recommend this method, with versatility

2,trim method

View copy print?
    1. $test = " dfadad 3333adf775fd  ";
    2. $test = Trim (Html_entity_decode ($test),chr (0xc2).   Chr (0xa0));
    3. Var_dump ($test);
    4. The results are as follows:
    5. String ' Dfadad 3333adf775fd ' (length=19)

This method is from the official PHP manual, if it is UTF8 code, using this method, there is no problem. If gbk,gb2312, it will appear garbled. There is also the json_encode, if this function is gbk,gb2312, the Chinese characters will be replaced with NULL. Recommended encoding for the use of UTF8

PHP 2 ways to turn the trailing blanks

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.