Php 如何检测UTF16解码

Php 如何检测UTF16解码,php,character-encoding,utf-16,utf,Php,Character Encoding,Utf 16,Utf,我必须读取一个文件并确定其解码类型,我使用了mb\u detect\u encoding()来检测utf-16,但得到了错误的结果。。如何在php中检测utf-16编码类型 Php文件是utf-16,我的头是windows-1256(因为是阿拉伯语) header('Content-Type:text/html;charset=windows-1256'); $delimiter='\t'; $f=文件(“$fileName”); foreach($f作为$dailystatmet) { $tr

我必须读取一个文件并确定其解码类型,我使用了
mb\u detect\u encoding()
来检测
utf-16
,但得到了错误的结果。。如何在php中检测
utf-16
编码类型

Php文件是utf-16,我的头是windows-1256(因为是阿拉伯语)

header('Content-Type:text/html;charset=windows-1256');
$delimiter='\t';
$f=文件(“$fileName”);
foreach($f作为$dailystatmet)
{
$transactionData=str_replace(“,”$dailystatmet);
preg_match_all(“/(”?\d+,\d+\.\d+)([a-zA-Z]|[0-9]|)[^.$delimiter.]+/”,$transactionData,$matches);
数组_push($matchesz,$matches[0]);
}
$searchKeywords=数组(“苹果”、“橙色”、“芒果”);
$rowCount=count($matchesz);

对于($row=1;$row如果有人仍在搜索解决方案,我在github上的“voku/portable-utf8”repo中黑客攻击了类似的东西

“file_get_contents”-包装器将通过“UTF8::str_detect_encoding()”检测当前编码,并将文件内容自动转换为UTF-8

e、 g:从PHPUnit测试中

$testString = UTF8::file_get_contents(dirname(__FILE__) . '/test1Utf16pe.txt');
$this->assertContains('<p>Today’s Internet users are not the same users who were online a decade ago. There are better connections.', $testString);

$testString = UTF8::file_get_contents(dirname(__FILE__) . '/test1Utf16le.txt');
$this->assertContains('<p>Today’s Internet users are not the same users who were online a decade ago. There are better connections.', $testString);
$testString=UTF8::file_get_contents(dirname(uuu file_uuu)。'/test1Utf16pe.txt');
$this->assertContains(“今天的互联网用户与十年前的互联网用户不同。有更好的连接。”,$testString);
$testString=UTF8::file_get_contents(dirname(uuu file_uuu)。'/test1utfle.txt');
$this->assertContains(“今天的互联网用户与十年前的互联网用户不同。有更好的连接。”,$testString);

我的解决方案是检测UTF-16并将代码转换为拉丁语15

  preg_match_all('/\x00/',$content,$count);
  if(count($count[0])/strlen($content)>0.4) {
     $content = iconv('UTF-16', 'ISO-8859-15', $content);
  }
换句话说,我检查十六进制字符00的频率。如果它高于0.4,则文本可能包含UTF-16编码的基集中的字符。这意味着字符有两个字节,但通常第二个字节是00

  preg_match_all('/\x00/',$content,$count);
  if(count($count[0])/strlen($content)>0.4) {
     $content = iconv('UTF-16', 'ISO-8859-15', $content);
  }