php – 如何区分可读的UTF-8字符串和二进制乱码?

前端之家收集整理的这篇文章主要介绍了php – 如何区分可读的UTF-8字符串和二进制乱码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
通过可读的UTF,我的意思是任何有效的UTF-8,而不是(当然)用户必须有一个字体来读取该字符串.
可读字符串的示例:
$readable_str0 = "Mary had a little lamb.";
$readable_str1 = "Příšerně žluťoučký kůň úpěl ďábelské ódy.";
$readable_str4 = "صِف خَلقَ خَودِ كَمِثلِ الشَمسِ إِذ بَزَغَت يَحظى الضَجيعُ بِها نَجلاءَ مِعطارِ";
$readable_str5 = "ཨ་ཡིག་དཀར་མཛེས་ལས་འཁྲུངས་ཤེས་བློའི་གཏེར༎"; //(Dzongkha)
$readable_str7 = "とりなくこゑす ゆめさませ みよあけわたる";
$readable_str8 = "TWFyeSBoYWQgYSBsaXR0bGUgbGFtYi4=";

不可读的字符串:

$not_readable_str0 = "�M,�T�HLQHT��,)�IU�I�M�";
$not_readable_str1 = "9��Příšerně žluťoučký kůň úpěl ďábelské ódy."
// this has some odd characters at the beginning so should count as unreadable
// it was result of gzdeflate of readable str 1
$not_readable_str4 = "ŹĎ5ůĹńŁV»×~1xâţöÚkkąő«¶’ŢáJ";
//some random selection from gif file
在某些情况下,很可能会失败的一种肮脏的黑客行为:
$str2 = iconv("UTF-8","UTF-8//IGNORE",$str);

并比较$str和$str2的长度.

原文链接:https://www.f2er.com/php/240221.html

猜你在找的PHP相关文章