A pit of the RTrim function

Source: Internet
Author: User
Briefly describe the problem you are experiencing today:
In my code there is the following code snippet:


    
         header("Content-type:text/html;charset=utf8");    echo rtrim("威、","、");

The original expectation is output "Wei". But backfired, output a few garbled. What is this for? Let me take it very carefully.
The first thing to know is the function of the RTrim function, and its second argument is a string, where each char will be taken out as the character to be removed. such as this:


    
     echo rtrim("blakefezabc", "cab");    //output blakefez

Yes, the output is Blakefez.
The implementation of this function should also be relatively simple:
①, starting from the last character of the first argument, iterates to the left to determine if the byte is in the second argument.
②, if present, remove the byte, and proceed to the first step. If it does not exist, it ends.
Please note that I use bytes above. Yes, this function is a non-multibyte-safe function. That is, when some of the characters in our arguments are multi-byte, they produce unwanted results. such as the beginning of the RTrim ("Wei,", ","). Because "Wei" and "," are UTF8 encoded. That is, multibyte characters. Wherein, "Granville" encoding is 0xe5 0xa8 0x81, "," The encoding is 0xe3 0x80 0x81, so, in the RTrim function eye, it sees is: RTrim ("0xe5 0xa8 0x81 0xe3 0x80 0x81", "0xe3 0x80 0x81" So the result of the final output is 0xe5 0xa8, and then converted to UTF8 code, it becomes garbled.

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes a pit of the RTrim function, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.