The phpsubstr () function intercepts Chinese strings with garbled characters.

Source: Internet
Author: User
In php, if I want to use substr () to intercept strings in full English, it would be a tragedy if it includes Chinese or English, but don't cut it off. we can use other methods to solve php interception.

In php, if I want to use substr () to intercept strings in full English, it would be a tragedy if it includes Chinese or English, however, we can use other methods to solve this problem.

Php intercepts Chinese strings and garbled characters. This was recently discovered. I have previously written an article about automatic generation of meta information, this article describes how many words are used as the description method before intercepting articles using php, but IE6 cannot load CSS. here is a supplement.

First of all, we need to clarify this problem. the reason why IE6 occasionally fails to load CSS is that the file is garbled and the link that loads CSS later cannot be correctly parsed by IE6, therefore, we can see a pure HTML page, without CSS, naked!

If the problem is identified, the remaining problem can be solved, that is, to prevent garbled code. Since the function provided by HiChina has garbled code, a php function is found again to solve the garbled code problem.

The substr () function can split text, but if the text to be split contains Chinese characters, it will often encounter problems.

The usage of the function mb_substr () is similar to that of substr (), but an additional parameter must be added at the end to set the encoding of the string, here we should understand the reasons for improving the HiChina method ~~ Next we will introduce several more advanced solutions.

Example 1 code:

  1. Function func_chgtitle ($ str, $ len) {// $ length the maximum length that can be displayed by a string
  2. $ Tmpstr = "";
  3. $ Strlen = $ len;
  4. For ($ I = 0; $ I <$ strlen; $ I ++ ){
  5. If (ord (substr ($ str, $ I, 1)> 0xa0 ){
  6. $ Tmpstr. = substr ($ str, $ I, 2 );
  7. $ I ++;
  8. } Else
  9. $ Tmpstr. = substr ($ str, $ I, 1 );
  10. }
  11. Return $ tmpstr;
  12. }

Example 2 string encoded as UTF-8, a Chinese character occupies three bytes:

  1. Public static function chinesesubstr ($ str, $ start, $ len) {// $ str indicates a string, and $ start indicates a string
  2.  
  3. Starting position. $ len indicates the string length.
  4. $ Strlen = $ start + $ len; // use $ strlen to store the total length of the string, that is, from the starting position of the string to the character
  5. Total length of a string
  6. The code is as follows:
  7. For ($ I = $ start; $ I <$ strlen ;){
  8. If (ord (substr ($ str, $ I, 1)> 0xa0) {// if the first byte of the string contains the ASCII ordinal number
  9. If the value is greater than 0xa0, it indicates Chinese characters.
  10. $ Tmpstr. = substr ($ str, $ I, 3); // each time a three-character is taken out and assigned to the variable $ tmpstr, that is, etc.
  11. In a Chinese character
  12. $ I = $ I + 3; // variable auto-Increment 3
  13. } Else {
  14. $ Tmpstr. = substr ($ str, $ I, 1); // if it is not a Chinese character, a character is taken out and assigned
  15. Variable $ tmpstr
  16. $ I ++;
  17. }
  18. }
  19. Return $ tmpstr; // return a string
  20. }

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.