• 技术文章 >php教程 >php手册

    简繁转换的程序

    2016-06-21 09:14:02原创603

    程序|简繁转换

    PHP代码:--------------------------------------------------------------------------------

    /**
    *中速版,中等内存使用,使用于一般需求或有大量重复字的大段文本
    *@text:待转换的字符串
    *@table_file:转换映射表文件名
    */
    function encode_trans1($text,$table_file='gb2big5') {
    $fp = fopen($table_file.'.table', "r");
    $cache = array();
    $max=strlen($text)-1;
    for($i=0;$i<$max;$i++) {
    $h=ord($text[$i]);
    if($h>=160) {
    $l=ord($text[$i+1]);
    if($h==161 && $l==64) {
    $text[$i]=" ";
    } else{
    $cut = substr($text,$i,2);
    if(!$cache[$cut]) {
    fseek($fp,($h-160)*510+($l-1)*2);
    $cache[$cut] = fread($fp,2);
    }
    $text[$i] = $cache[$cut][0];
    $text[++$i] = $cache[$cut][1];
    }
    }
    }
    fclose($fp);
    return $text;
    }

    /**
    *低速版,最低内存使用,使用于少量字符时
    *@text:待转换的字符串
    *@table_file:转换映射表文件名
    */
    function encode_trans2($text,$table_file='gb2big5') {
    $fp = fopen($table_file.'.table', "r");
    $max=strlen($text)-1;
    for($i=0;$i<$max;$i++) {
    $h=ord($text[$i]);
    if($h>=160) {
    $l=ord($text[$i+1]);
    if($h==161 && $l==64) {
    $gb=" ";
    }else{
    fseek($fp,($h-160)*510+($l-1)*2);
    $gb=fread($fp,2);
    }
    $text[$i]=$gb[0];
    $text[$i+1]=$gb[1]; $i++;
    }
    }
    fclose($fp);
    return $text;
    }
    /**
    *高速版,最高内存使用,使用于大段文本时
    *@text:待转换的字符串
    *@table_file:转换映射表文件名
    */
    function encode_trans3($text,$table_file='gb2big5') {
    $fp = fopen($table_file.'.table', "r");
    $str = fread($fp,strlen($table_file.'.table'));
    fclose($fp);
    $max=strlen($text)-1;
    for($i=0;$i<$max;$i++) {
    $h=ord($text[$i]);
    if($h>=160) {
    $l=ord($text[$i+1]);
    if($h==161 && $l==64) {
    $text[$i]=' ';
    $text[++$i]=' ';
    }else{
    $pos = ($h-160)*510+($l-1)*2;
    $text[$i]=$str[$pos];
    $text[++$i]=$str[$pos+1];
    }
    }
    }
    return $text;
    }
    ?>

    --------------------------------------------------------------------------------





    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:text table quot file cache
    上一篇:经验积累,献给PHP爱好者!!! 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 建立文件交换功能的脚本(二)• PHP教程.应用实例2 • php 简单的缓存全站函数介绍• paip.session的调试in php• CI框架源码阅读---------Input.php
    1/1

    PHP中文网