Table of Contents
2. [代码]使用方法    
Home php教程 PHP源码 不使用mb_substr函数切割字符串函数

不使用mb_substr函数切割字符串函数

May 25, 2016 pm 05:12 PM

跳至

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

function cutstr($string, $length, $dot = ' ...')

{

    global $charset;

 

    if(strlen($string) <= $length) return $string;

 

    $string = str_replace(array(&#39;&&#39;, &#39;"&#39;, &#39;<&#39;, &#39;>&#39;), array(&#39;&&#39;, &#39;"&#39;, &#39;<&#39;, &#39;>&#39;), $string);

 

    $strcut = &#39;&#39;;

     

    if(strtolower($charset) == &#39;utf-8&#39;)

    {

        $n = $tn = $noc = 0;

         

        while($n < strlen($string))

        {

            $t = ord($string[$n]);

 

            // 特别要注意这部分,utf-8是1--6位不定长表示的,这里就是如何

            // 判断utf-8是1位2位还是3位还是4、5、6位,这对其他语言的编程也有用处

            // 具体可以查看rfc3629或rfc2279

            if($t == 9 || $t == 10 || (32 <= $t && $t <= 126))

            {

                $tn = 1; $n++; $noc++;

            }

            elseif(194 <= $t && $t <= 223)

            {

                $tn = 2; $n += 2; $noc += 2;

            }

            elseif(224 <= $t && $t < 239)

            {

                $tn = 3; $n += 3; $noc += 2;

            }

            elseif(240 <= $t && $t <= 247)

            {

                $tn = 4; $n += 4; $noc += 2;

            }

            elseif(248 <= $t && $t = $length)

            {

                break;

            }

 

        }

         

        if($noc > $length) $n -= $tn;

 

        $strcut = substr($string, 0, $n);

    }

    else

    {

        for($i = 0; $i < $length; $i++)

        {

            $strcut .= ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];

        }

    }

 

    $strcut = str_replace(array(&#39;&&#39;, &#39;"&#39;, &#39;<&#39;, &#39;>&#39;), array(&#39;&&#39;, &#39;"&#39;, &#39;<&#39;, &#39;>&#39;), $strcut);

 

    return $strcut . $dot;

}

Copy after login

2. [代码]使用方法

1

2

3

4

5

6

7

8

$testStr = "好好学习!(good good study)天天向上!(day day up)";     // ^_^! V

 

// 使用utf文档

echo cutstr($testStr, 10);

 

// 使用dos文档使用以下代码测试utf-8的效果

//$testStr = iconv(&#39;GBK&#39;, &#39;UTF-8&#39;, $testStr);

//echo iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, cutstr($testStr, 10));

Copy after login

           


       

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)