Php methods for intercepting Chinese characters without garbled characters

Source: Internet
Author: User
This article mainly introduces php's method of intercepting Chinese characters by character without garbled characters. It involves php's skills in operating Chinese and English strings. It is of great practical value. For more information, see

This article mainly introduces php's method of intercepting Chinese characters by character without garbled characters. It involves php's skills in operating Chinese and English strings. It is of great practical value. For more information, see

This example describes how php intercepts Chinese characters without garbled characters. Share it with you for your reference. The specific implementation method is as follows:

<? Php // $ str string to be intercepted // $ len number of characters to be intercepted // $ chars number of characters already intercepted // $ res saved string // $ chars saved already intercepted the number of strings // The offset intercepted by $ offset // The number of bytes of the $ length string // if the number of characters of $ len> $ str, result in unnecessary while LOOP, ($ offset <$ length limit) function utf8sub ($ str, $ len) {if ($ len <= 0) {return ;} $ res = ""; $ offset = 0; $ chars = 0; $ length = strlen ($ str); while ($ chars <$ len & $ offset <$ length) {$ hign = decbin (ord (substr ($ str, $ offset, 1); if (strlen ($ hign) <8) {$ count = 1 ;} elseif (substr ($ hign, 0, 3) = "110") {$ count = 2;} elseif (substr ($ hign, 1110) = "") {$ count = 3 ;} elseif (substr ($ hign, 11110) = "111110") {$ count = 4;} elseif (substr ($ hign,) = ") {$ count = 5;} elseif (substr ($ hign, 0,7) = "1111110") {$ count = 6;} $ res. = substr ($ str, $ offset, $ count); $ offset + = $ count; $ chars + = 1;} return $ res;} function utf8sub1 ($ str, $ len) {$ chars = 0; $ res = ""; $ offset = 0; $ length = strlen ($ str ); while ($ chars <$ len & $ offset <$ length) {$ Hign = decbin (ord (substr ($ str, $ offset, 1); if (strlen ($ hign) <8) {$ count = 1 ;} elseif ($ hign & "11100000" = "11000000") {$ count = 2;} elseif ($ hign & "11110000" = "11100000 ") {$ count = 3;} elseif ($ hign & "11111000" = "11110000") {$ count = 4 ;} elseif ($ hign & "11111100" = "11111000") {$ count = 5;} elseif ($ hign & "11111110" = "11111100 ") {$ count = 6;} $ res. = substr ($ str, $ offset, $ count); $ chars ++; $ offset + = $ count;} return $ res ;}$ a = "Chinese ah people Min hdj "; echo utf8sub ($ a, 5);?>

I hope this article will help you with php programming.

,

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.