Php solves the problem of garbled characters intercepted by substr ()

Source: Internet
Author: User
This article mainly introduces the PHPsubstr () function. Here we provide code examples to illustrate how to use substr () in php to intercept Chinese strings in garbled characters () it's okay to intercept all strings in English. if there are any Chinese or English characters, it will be a tragedy, but 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 for loading 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

The code is as follows:

Function func_chgtitle ($ str, $ len) {// $ length the maximum length of the string that we allow to display $ tmpstr = ""; $ strlen = $ len; for ($ I = 0; $ I <$ strlen; $ I ++) {if (ord (substr ($ str, $ I, 1)> 0xa0) {$ tmpstr. = substr ($ str, $ I, 2); $ I ++;} else $ tmpstr. = substr ($ str, $ I, 1);} return $ tmpstr ;}

Example 2

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

Public static function chinesesubstr ($ str, $ start, $ len) {// $ str indicates the string, $ start indicates the start position of the string, and $ len indicates the string length.

$ Strlen = $ start + $ len; // use $ strlen to store the total length of the string, that is, from the start position of the string to the total length of the string

The code is as follows:

For ($ I = $ start; $ I <$ strlen;) {if (ord (substr ($ str, $ I, 1)> 0xa0) {// if the value of the first byte in the string is greater than 0xa0, it indicates the Chinese character $ tmpstr. = substr ($ str, $ I, 3); // each time a three-character string is taken out and assigned to the variable $ tmpstr, that is, a Chinese character $ I = $ I + 3; // variable auto-Increment 3} else {$ tmpstr. = substr ($ str, $ I, 1); // if it is not a Chinese character, a character is taken out each time and assigned to the variable $ tmpstr $ I ++;} return $ tmpstr; // return string}

This article is intended to help you solve the same problem in PHP programming!

For more articles about how php solves the problem of substr () intercepting Chinese characters and garbled characters, refer to PHP Chinese website!

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.