Home >Backend Development >PHP Tutorial >Program for converting Simplified Chinese to Traditional Chinese_PHP tutorial

Program for converting Simplified Chinese to Traditional Chinese_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:12:35884browse

PHP代码:--------------------------------------------------------------------------------
/**
*Medium speed version, medium memory usage, used for general needs or large text with a large number of repeated words
*@text: string to be converted
*@table_file: conversion mapping table file name
*/
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;
}
/**
*Low-speed version, lowest memory usage, used when a small number of characters
*@text: string to be converted
*@table_file: conversion mapping table file name
*/
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;
}
/**
*高速版,最高内存使用,使用于大段文本时

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/629338.htmlTechArticlePHP代码:-------------------------------------------------------------------------------- ?php /** *中速版,中等内存使用,使用于一般需求或有大量重复字的大段...
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