Simple and traditional conversion program

Source: Internet
Author: User
PHP code :--------------------------------------------------------------------------------? Php/*** medium-speed version, medium-memory usage, used for large text segments that generally require or have a large number of repeated words * @ text: string to be converted * @ table_file: conversion ing table file name * PHP code :--------------------------------------------------------------------------------

/**
* Medium speed edition, medium memory usage, used for large text segments that generally require or have a large number of repeated words
* @ Text: string to be converted
* @ Table_file: name of the conversion ing 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;
}

/**
* Low-speed version, with the lowest memory usage, when using a few characters
* @ Text: string to be converted
* @ Table_file: name of the conversion ing 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;
}
/**
* High-speed edition with maximum memory usage, used for large text segments
* @ Text: string to be converted
* @ Table_file: name of the conversion ing 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;
}
?>

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



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.