PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » PHP中高级 » 任意字符集下正常显示网页的方法
本页主题: 任意字符集下正常显示网页的方法 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

自由的龙

该用户目前不在线
级别: 中级程序员
精华: 1
发帖: 1264
威望: 1267 点
金钱: 12660 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2006-04-16
最后登录:2006-06-27

任意字符集下正常显示网页的方法


<?php
// 在任意字符集下正常显示网页的方法
// 测试通过
// 可以在存入数据库前使用

error_reporting(E_ALL);

if(function_exists('mb_convert_encoding')) {// 需要有 Multibyte String 扩展
    $a = mb_convert_encoding('你好', 'HTML-ENTITIES', 'GB2312');
    $b = mb_convert_encoding($a, 'GB2312', 'HTTP-ENTITIES');
    wFile('try.php', $a.'
'.$b);
    /*如果需要对整个页面进行转换,只需要在PHP文件头加上下面三行
    mb_internal_encoding('gb2312');
    mb_http_output('HTML-ENTITIES');
    ob_start('mb_output_handle');
    */
} elseif(function_exists('iconv')) {
    $a = uiconv('gb2312', 'a,世界您好!$%#abc');
    wFile('try.php', $a);
    print $a;
} else print 'sorry my baby';

function wFile($file, $content) {
    $fp = fopen($file, 'w');
    flock($fp, 2);
    fwrite($fp, $content);
    flock($fp, 3);
    fclose($fp);
}

// 使用 iconv 对页面进行转换--测试通过
function uiconv($encode, $string) {
    $string = iconv($encode, 'UTF-16BE', $string);
    $output = '';
    for($i = 0; $i < strlen($string); $i++,$i++) {
          $code = ord($string{$i}) * 256 + ord($string{$i + 1});
          if($code < 128) {
                $output .= chr($code);// chr 是把英文或是半角符号转换为字符
          } elseif($code != 65279) {
                $output .= '&#'.$code.';';
          }
    } return $output;
}
?>
顶端 Posted: 2006-04-25 14:14 | [楼 主]
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » PHP中高级

时:11-23 20:11 Copyright © 2006 phpwhy.com 权
ICP05060669

曳息 -