当前位置: 首页 > Web开发 > PHP教程 >正文

php中big5转utf8乱码怎么办?

来源:互联网时间:2020-07-13 14:20:04编辑:网友分享
php中big5转utf8乱码的解决办法:首先生成tab文件,并生成时要保证tab文件不存在;然后将指定页面转码测试;接着打印出文字库;最后big5转【utf-8】即可。php中big5转utf8乱码的解决

php中big5转utf8乱码的解决办法:首先生成tab文件,并生成时要保证tab文件不存在;然后将指定页面转码测试;接着打印出文字库;最后big5转【utf-8】即可。

php中big5转utf8乱码怎么办?

php中big5转utf8乱码的解决办法:

第一步:生成tab文件,生成时要保证tab文件不存在才可以

writebig5UnicodeFile();


第二步:指定页面转码测试

testCode();


第三步:打印出文字库

printfCode();


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

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

<?php

//生成big5-unicode 编码文件

function loadBig5(){

    $fp = fopen( './big5-unicode.txt', 'r' );

    $big5_unicode_arr = array();

    while($one_line = fgets($fp)) {

        $one_line_arr = explode("\t",$one_line);

        $big5 = hexdec(trim($one_line_arr[0]));

        $unicode = trim($one_line_arr[1]);

        if(strpos($unicode,',')) {

            $unicode = ltrim(explode(',',$unicode)[0],'<');

        }

         

        $big5_unicode_arr[$big5] = hexdec($unicode);

    }

     

    return $big5_unicode_arr;

}

  

//追加形式写入文件

function putContent($content) {

    static $fp;

    if(!isset($fp)) {

        $fp = fopen( './big5-unicode-new.tab', 'a+' );

    }

     

    fwrite($fp,$content);

}

  

//生成tab文件

function writebig5UnicodeFile() {

    $big5_unicode_arr = loadBig5();

    $big5_unicod_content = array();

    $min = 2000;

    $max = 0;

    $max_unicode = 0;

    foreach($big5_unicode_arr as $big5 => $unicode) {

        $h = floor($big5/256);

        $l = $big5%256;

        $index = ($h-135)*256*3+$l*3;

         

        if($index<$min) {

            $min = $index;

        }

         

        if($max<$index) {

            $max = $index;

        }

         

        if($unicode>$max_unicode) {

            $max_unicode = $unicode;

        }

         

        $h_1 = floor($unicode/65536);

        $h_2 = floor($unicode/256);

        $h_3 = $unicode%256;

         

        $big5_unicod_content[$index] = chr($h_1).chr($h_2).chr($h_3);

    }

     

    for($i=0;$i<=$max;$i=$i+3) {

        if(!isset($big5_unicod_content[$i])) {

            $big5_unicod_content[$i] = chr(0).chr(0).chr(0);

        }

    }

     

    for($i=0;$i<=$max;$i=$i+3) {

        if(strlen($big5_unicod_content[$i]) == 3) {

            putContent($big5_unicod_content[$i]);

        }else{

            die('error');

        }

    }

}

  

//测试编辑结果

function testCode() {

    $content = file_get_contents( './temlate_2.html');

    echo b2u($content);

}

  

//打印出编码库文字

function printfCode() {

    $fp = fopen( './big5-unicode-new.tab', 'r' );

    $len = filesize('./big5-unicode-new.tab');

    $x = 0;

    $outstr = array();

    //     fseek( $fp, 21000 - 900 + 42*3);

    for($i=$x=0;$i<$len;$i=$i+3) {

        $uni = fread( $fp, 3 );

        $codenum = ord($uni[0])*65536 + ord($uni[1])*256 + ord($uni[2]);

        if($codenum == 0) {

            $outstr[$x++] = ' ';

        }elseif( $codenum < 0x80 ) {

            $outstr[$x++] = chr($codenum);

        }elseif($codenum < 0x800) {

            $outstr[$x++] = chr( 192 + $codenum / 64 );

            $outstr[$x++] = chr( 128 + $codenum % 64 );

  

        }elseif($codenum < 0x10000){

            $outstr[$x++] = chr( 224 + floor($codenum / 4096 ));

            $codenum = $codenum%4096;

            $outstr[$x++] = chr( 128 + floor($codenum / 64 ));

            $outstr[$x++] = chr( 128 + ($codenum % 64) );

        }else{

            $outstr[$x++] = chr( 240 + floor($codenum / 262144 ));

            $codenum = $codenum%262144;

            $outstr[$x++] = chr( 128 + floor($codenum / 4096 ));

            $codenum = $codenum%4096;

            $outstr[$x++] = chr( 128 + ($codenum / 64) );

            $outstr[$x++] = chr( 128 + ($codenum % 64) );

        }

    }

  

    echo join( '', $outstr);

}

  

//big5 转 utf-8

function b2u( $instr ) {

    $fp = fopen( './big5-unicode-new.tab', 'r' );

    $len = strlen($instr);

    $outstr = '';

    for( $i = $x = 0 ; $i < $len ; $i++ ) {

        $h = ord($instr[$i]);

        if( $h >= 135 ) {

            $l = ord($instr[$i+1]);

             

            fseek( $fp, ($h-135)*256*3+$l*3 );

            $uni = fread( $fp, 3 );

             

            $codenum = ord($uni[0])*65536 + ord($uni[1])*256 + ord($uni[2]);

             

            if($codenum == 0) {

                $outstr[$x++] = ' ';

            }elseif( $codenum < 0x80 ) {

                $outstr[$x++] = chr($codenum);

            }elseif($codenum < 0x800) {

                $outstr[$x++] = chr( 192 + $codenum / 64 );

                $outstr[$x++] = chr( 128 + $codenum % 64 );

                 

            }elseif($codenum < 0x10000){

                $outstr[$x++] = chr( 224 + floor($codenum / 4096 ));

                $codenum = $codenum%4096;

                $outstr[$x++] = chr( 128 + floor($codenum / 64 ));

                $outstr[$x++] = chr( 128 + ($codenum % 64) );

            }else{

                $outstr[$x++] = chr( 240 + floor($codenum / 262144 ));

                $codenum = $codenum%262144;

                $outstr[$x++] = chr( 128 + floor($codenum / 4096 ));

                $codenum = $codenum%4096;

                $outstr[$x++] = chr( 128 + ($codenum / 64) );

                $outstr[$x++] = chr( 128 + ($codenum % 64) );

            }

            $i++;

        }

        else

            $outstr[$x++] = $instr[$i];

    }

    fclose($fp);

    if( $instr != '' )

        return join( '', $outstr);

}


上一篇: 两分钟了解php如何获取header头

下一篇:学习PHP实现SMTP邮件的发送

您可能感兴趣的文章

相关阅读