The program _php of simple and complicated conversion
Last Update:2016-06-01
Source: Internet
Author: User
Keywords
program
conversion
Text
fp
if
max
use
ord
c
PHP Code:--------------------------------------------------------------------------------
/**
* Medium-speed version, moderate memory usage, for general needs or large pieces of text with a large number of repeating words
* @text: String to convert
* @table_file: Convert 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) * *);
$cache [$cut] = Fread ($fp, 2);
}
$text [$i] = $cache [$cut][0];
$text [+ + $i] = $cache [$cut][1];
}
}
}
Fclose ($FP);
return $text;
}
/**
* Low speed version, minimum memory usage, for use in small numbers of characters
* @text: String to convert
* @table_file: Convert 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) * *);
$GB =fread ($FP, 2);
}
$text [$i]= $gb [0];
$text [$i +1]= $GB [1]; $i + +;
}
}
Fclose ($FP);
return $text;
}
/**
* High speed version, maximum memory usage, when used for large pieces of text
* @text: String to convert
* @table_file: Convert mapping Table file name
*/
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) * *;
$text [$i]= $str [$pos];
$text [+ + $i]= $str [$pos +1];
}
}
}
return $text;
}
?>
--------------------------------------------------------------------------------