UTF-8编码的文件分为带Bom和不带Bom两种,带Bom的大家都很容易处理,不带Bom的会有点麻烦,所以写了一个函数去判断,代码如下:
//返回 1 表示纯 ASCII(即是所有字符都不大于127)
//返回 2 表示UTF8
//返回 0 表示正常gb编码
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;
}
}
波比源码 » 用PHP判断文件是否是UTF-8编码(检查Bom)
Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. Hacklink , Hacklink panel , Hacklink al Hacklink panel
I really love to read such an excellent article. Helpful article. Hello Administ . Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
Great post thank you. Hello Administ . Onwin , Onwin Giriş , Onwin Güncel Giriş , Onwin Yeni Adres , onwin
Everything is very open and very clear explanation of issues. was truly information. Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
152829 742770hey very good web site i will definaely come back and see once more. 279935
I’m not that much of a internet reader to be honest but your blogs
really nice, keep it up! I’ll go ahead and bookmark your website to come back down the road.
Cheers
Howdy! Do you use Twitter? I’d like to follow you
if that would be okay. I’m definitely enjoying your blog and look forward to new updates.
This is a topic that’s near to my heart… Thank you! Exactly where are your contact details though?
You should be a part of a contest for one of the greatest sites on the internet.
I am going to recommend this blog!
Terrific post however , I was wanting to know if you could write
a litte more on this topic? I’d be very thankful
if you could elaborate a little bit further. Cheers!
Spot on with this write-up, I really believe this site needs a great deal more attention. I’ll probably be returning to
read more, thanks for the advice!
Hey I know this is off topic but I was wondering if you knew of any widgets I could
add to my blog that automatically tweet my
newest twitter updates. I’ve been looking for a plug-in like
this for quite some time and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything.
I truly enjoy reading your blog and I look forward to
your new updates.
Very good post. I’m dealing with many of these issues as well..
Hey there would you mind letting me know which
webhost you’re working with? I’ve loaded your blog in 3 completely different web browsers and I must
say this blog loads a lot quicker then most. Can you suggest a good internet hosting provider
at a reasonable price? Thanks, I appreciate it!
I seriously love your blog.. Great colors & theme.
Did you build this web site yourself? Please reply back as I’m wanting to create my very own site and would love
to learn where you got this from or what the theme is named.
Cheers!
Very nice article. I definitely appreciate this website.
Continue the good work!
Hey there! Would you mind if I share your blog with my twitter group?
There’s a lot of folks that I think would really enjoy your content.
Please let me know. Many thanks
Hmm is anyone else having problems with the pictures on this blog loading?
I’m trying to determine if its a problem on my end or if it’s
the blog. Any responses would be greatly appreciated.
Hello mates, how is the whole thing, and what you would like to say about this post, in my view its
actually awesome in support of me.
I am genuinely glad to read this webpage posts which contains
lots of useful data, thanks for providing these kinds of data.