The program _php of simple and complicated conversion

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

--------------------------------------------------------------------------------
  • 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.