Home >Backend Development >PHP Tutorial >How to intercept Chinese characters by characters without garbled characters_PHP tutorial

How to intercept Chinese characters by characters without garbled characters_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:00:001122browse

How to intercept Chinese characters by characters without garbled characters in php

This article mainly introduces the method of intercepting Chinese characters by characters without garbled characters in php, involving the techniques of operating Chinese and English strings in php. It is of great practical value, friends in need can refer to it

The example in this article describes the method of intercepting Chinese characters by characters without garbled characters in PHP. Share it with everyone 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

//$str string to be intercepted

//$len The number of characters intercepted

//$chars The number of characters that have been intercepted

//$res saved string

//$chars saves the number of intercepted strings

//$offset intercepted offset

//$length The number of bytes in the string

//If $len>$str’s number of characters, it will cause a meaningless 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,0,4)=="1110"){

$count=3;

}elseif(substr($hign,0,5)=="11110"){

$count=4;

}elseif(substr($hign,0,6)=="111110"){

$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="中华ah人hdj";

echo utf8sub($a,5);

?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975121.htmlTechArticleHow to intercept Chinese characters by characters without garbled characters in php. This article mainly introduces the method of intercepting Chinese characters by characters in php without garbled characters. , involving the skills of operating Chinese and English strings in PHP, which are of great practical value...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn