The program of simple multiplication conversion

Source: Internet
Author: User
Tags fread memory usage ord php code return strlen version
program | Simplified conversion PHP Code:--------------------------------------------------------------------------------

<?php
/**
* Medium-speed version, medium memory use, use for general requirements or large text with a large number of repeated words
* @text: strings to be converted
* @table_file: Converting the 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, minimum memory use, for a small number of characters characters
* @text: strings to be converted
* @table_file: Converting the 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;
}
/**
* High-speed version, maximum memory usage, when used for large segment text
* @text: strings to be converted
* @table_file: Converting the 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) *2;
$text [$i]= $str [$pos];
$text [+ + $i]= $str [$pos +1];
}
}
}
return $text;
}
?>

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





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.