Use PHP to determine if a file is UTF-8 encoded (check BOM) _php Tutorial

Source: Internet
Author: User
UTF-8 encoded files are divided into BOM and without BOM two kinds, with the BOM everyone is easy to handle, without BOM will be a little trouble, so write a function to judge, the code is as follows:

Returns 1 for pure ASCII (that is, all characters are not greater than 127)
Returns 2 indicating UTF8
Return 0 indicates normal GB encoding

function TestUtf8 ($text)
{
if (strlen ($text) < 3) return false;
$lastch = 0;
$begin = 0;
$BOM = true;
$BOMchs = Array (0xEF, 0xBB, 0xBF);
$good = 0;
$bad = 0;
$notAscii = 0;
for ($i =0; $i < strlen ($text); $i + +)
{
$ch = Ord ($text [$i]);
if ($begin < 3)
{
$BOM = ($BOMchs [$begin]== $ch);
$begin + = 1;
Continue
}

if ($begin ==4 && $BOM) break;

if ($ch >= 0x80) $notAscii + +;

if (($ch &0xc0) = = 0x80)
{
if (($lastch &0xc0) = = 0xC0)
{
$good + = 1;
}
else if (($lastch &0x80) = = 0)
{
$bad + = 1;
}
}
else if (($lastch &0xc0) = = 0xC0)
{
$bad + = 1;
}
$lastch = $ch;
}
if ($begin = = 4 && $BOM)
{
return 2;
}
else if ($notAscii ==0)
{
return 1;
}
else if ($good >= $bad)
{
return 2;
}
Else
{
return 0;
}
}

http://www.bkjia.com/PHPjc/364705.html www.bkjia.com true http://www.bkjia.com/PHPjc/364705.html techarticle UTF-8 encoded files are divided into BOM and without BOM two kinds, with the BOM is easy to handle, without BOM will be a little trouble, so write a function to judge, the code is as follows://return ...

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