Php methods for intercepting Chinese characters without garbled characters

Source: Internet
Author: User

Php methods for intercepting Chinese characters without garbled characters

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

<? Php

// $ Str string to be intercepted

// The number of characters intercepted by $ len

// $ Number of characters intercepted by chars

// $ Res saved string

// $ Chars saves the number of intercepted strings

// $ Offset

// $ Length the number of bytes of the string

// If the number of characters in $ len> $ str causes unnecessary while loops ($ 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, 110) = "){

$ Count = 2;

} Elseif (substr ($ hign, 1110) = "){

$ Count = 3;

} Elseif (substr ($ hign, 11110) = "){

$ Count = 4;

} Elseif (substr ($ hign, 111110) = "){

$ Count = 5;

} Elseif (substr ($ hign, 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 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.