Php coding and conversion implementation code (1/6) _ PHP Tutorial

Source: Internet
Author: User
Php coding and conversion implementation code (16 ). We provide several types of encoding conversion, such as UTF-8 to GB encoding GB to UTF-8 encoding Unicode to utf8unicodeurl encoding to gbk encoding function GB code to convert Big5 code Big5 code to GB code utf8 to Unico we provides several types of encoding conversion, such as UTF-8 to GB encoding GB to UTF-8 encoding Unicode to utf8 unicode url encoding to gbk encoding function GB code conversion to Big5 code Big5 code to GB code utf8 to Unicode wait.

We provide several types of encoding conversion, such as UTF-8 to gb encoding gb to UTF-8 encoding unicode to utf8 unicode url encoding to gbk encoding function gb to convert big5 to big5 to UTF-8 unicode.
*/

$ Uc2gbtable = $ codetable = $ big5_data = $ gb_data = '';
$ Gbkunidic = null;

// UTF-8 to gb encoding
Function utf82gb ($ utfstr)
{
If (function_exists ('iconv '))
{
Return iconv ('utf-8', 'gbk // ignore ', $ utfstr );
}
Global $ uc2gbtable;
$ Okstr = "";
If (trim ($ utfstr) = "")
{
Return $ utfstr;
}
If (empty ($ uc2gbtable ))
{
$ Filename = dedeinc. "/data/gb2312-utf8.dat ";
$ Fp = fopen ($ filename, "r ");
While ($ l = fgets ($ fp, 15 ))
{
$ Uc2gbtable [hexdec (substr ($ l, 7, 6)] = hexdec (substr ($ l, 0, 6 ));
}
Fclose ($ fp );
}
$ Okstr = "";
$ Ulen = strlen ($ utfstr );
For ($ I = 0; $ I <$ ulen; $ I ++)
{
$ C = $ utfstr [$ I];
$ Cb = decbin (ord ($ utfstr [$ I]);
If (strlen ($ cb) = 8)
{
$ Csize = strpos (decbin (ord ($ cb), "0 ");
For ($ j = 0; $ j <$ csize; $ j ++)
{
$ I ++; $ c. = $ utfstr [$ I];
}
$ C = utf82u ($ c );
If (isset ($ uc2gbtable [$ c])
{
$ C = dechex ($ uc2gbtable [$ c] + 0x8080 );
$ Okstr. = chr (hexdec ($ c [0]. $ c [1]). chr (hexdec ($ c [2]. $ c [3]);
}
Else
{
$ Okstr. = "& #". $ c .";";
}
}
Else
{
$ Okstr. = $ c;
}
}
$ Okstr = trim ($ okstr );
Return $ okstr;
}

1 2 3 4 5 6

Refer to GB encoding GB to UTF-8 encoding Unicode to utf8 unicode url encoding to gbk encoding function GB code to convert Big5 code Big5 code to GB code utf8 to Unico...

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.