PHP functions that convert traditional Chinese to simplified Chinese

Source: Internet
Author: User

I asked the OSO forum a few days ago if there is any source. Program No response. Finally, I made up my mind to transplant such a function. Thanks to the user Keyes for providing the Delphi for porting. Source code . The call method is $ TXT = big5togb ($ txt ).
(Note: Source Code Include "data_big5.php"; this file is an array, in bytes .)

<?
/*************************************** ********************************
Written by Caocao
Caocao@eastday.com
Http://caocao.oso.com.cn

With the help of Keyes
Keyes2000@263.net
Http://my-wjl.scu.edu.cn /~ Keyes
**************************************** *******************************/
Function isbig5 ($ code)
{
If (strlen ($ code)> = 2)
{
$ Code = strtok ($ code ,"");

If (ord ($ code [0]) <161)
{
Return (0 );
}
Else
{
If (ord ($ code [1])> = 64) & (ord ($ code [1]) <= 126 )) | (ord ($ code [1])> = 161) & (ord ($ code [1]) <= 254 )))
{
Return (1 );
}
Else
{
Return (0 );
}
}
}
Else
{
Return (0 );
}
}

Function big5offset ($ code)
{
If (strlen ($ code)> = 2)
{
$ Code = strtok ($ code ,"");
If (ord ($ code [1]) >=64) & (ord ($ code [1]) <= 126 ))
{
Return (ord ($ code [0])-161) * 157 + (ord ($ code [1])-64 ));
}
If (ord ($ code [1])> = 161) & (ord ($ code [1]) <= 254 ))
{
Return (ord ($ code [0])-161) * 157 + 63 + (ord ($ code [1])-161 ));
}
}
Return (-1 );
}

Function wordtostring ($ code)
{
Return (CHR (hexdec (substr ($ code,). CHR (hexdec (substr ($ code ))));
}

Function big5togb ($ code)
{
Include "data_big5.php ";
$ Output = "";
$ Length = strlen ($ Code );
$ Code = strtok ($ code ,"");
$ Idx = 0;
While ($ idx <$ length)
{
$ Tmpstr = $ code [$ idx]. $ code [$ idx + 1];

If (isbig5 ($ tmpstr ))
{
$ Offset = big5offset ($ tmpstr );
If ($ Offset> = 0) | ($ offset <= 14757 ))
{
$ Output. = wordtostring ($ big5order [$ offset]);
$ Idx ++;
}
Else
{
$ Output. = $ code [$ idx];
}
}
Else
{
$ Output. = $ code [$ idx];
}
$ Idx ++;
}
Return ($ output );
}
?>

[This article is copyrighted by the author and osuo. If you need to reprint it, please indicate the author and its source]

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.