About PHP solve substr () intercept Chinese characters garbled problem solving method

Source: Internet
Author: User
This article mainly introduces the PHP substr () function, here is a code example to illustrate when the Chinese character string truncation problem, the need for small partners can refer to the following

In PHP if I want to use substr () to intercept the string in English is no problem, if included in Chinese or English will be tragic, but we also do not cut we can use other methods to solve.

PHP intercept Chinese characters garbled, this is the recent discovery of things, previously I wrote an article about the automatic generation of meta information, the article about the use of PHP to intercept the article before the number of words as Description method, but there is a IE6 can not load CSS phenomenon, here to do a supplement.

First of all to clarify such a problem, the reason is IE6 occasionally unable to load the phenomenon of CSS, because the file is garbled, resulting in the subsequent loading of the CSS link can not be IE6 correctly parsed. So you see a plain HTML page, no CSS, naked! Clear the problem, the rest of the problem is good to solve, is to prevent garbled, since the function provided by Wango has garbled, went to find a PHP function to solve this garbled problem.

The substr () function splits the text, but the text you want to split can often have problems if you include Chinese characters.

MB_SUBSTR () The use of this function is similar to that of substr (), except that one more parameter is added at the end to set the encoding of the string.

By the way, you should understand why I have improved the Million-GE method ~ ~

Here are a few more advanced approaches

Example 1

The code is as follows

function Func_chgtitle ($STR, $len) {//$length we allow the maximum length of the string 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

The string is encoded as UTF-8, and a Chinese character occupies three bytes:

Public static function Chinesesubstr ($str, $start, $len) {//$str refers to a string, $start refers to the starting position of the string, $len refers to the string length

$strlen = $start + $len ; Stores the total length of a string, from the starting position of the string to the total length of the string, with $strlen

The code is as follows

for ($i = $start; $i < $strlen;)  {if (Ord (substr ($str, $i, 1)) > 0xa0) {//If the ASCII ordinal value of the first byte in the string is greater than 0xa0, the kanji $tmpstr. = substr ($str, $i, 3);// Each time a three-bit word is taken out assigned to the variable $tmpstr, which equals a Chinese character $i = $i +3; Variable 3} else{$tmpstr. = substr ($str, $i, 1);//If it is not a Chinese character, each time you remove one word assigned give the variable $tmpstr $i + +;}} return $tmpstr; Return string}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.