Home Backend Development PHP Tutorial 简繁转换的程序_PHP

简繁转换的程序_PHP

Jun 01, 2016 pm 12:39 PM
c if max ord text use program Convert

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 $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 $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 $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;
}
?>

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



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 Article

Hot Article

Hot Article Tags

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)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

How to make Google Maps the default map in iPhone

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

What software is crystaldiskmark? -How to use crystaldiskmark?

How to write if in c language to judge multiple conditions How to write if in c language to judge multiple conditions Mar 25, 2024 pm 03:24 PM

How to write if in c language to judge multiple conditions

Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width form

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

How to download foobar2000? -How to use foobar2000

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

How to use NetEase Mailbox Master

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Clock app missing in iPhone: How to fix it

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

How to use Baidu Netdisk app

See all articles