2017-04-11 5 views
-4

を使用してファイルのエンコーディングを検索する私はPHPを使用してファイルのエンコーディングを見つけようとしていますが、私はカントは、解決策を得るように見える私はリストから検出するために、次のコードを使用した溶液どのようにPHP

がありますエンコーディングの

public function detect($filePath) 
{ 
     $fopen=fopen($filePath,'r'); 

     $row = fgets($fopen); 
     $encodings = mb_list_encodings(); 
     $encoding = mb_detect_encoding($row, "UTF-8, ASCII, Windows-1252, Windows-1254, Windows-1255");//these are my favorite encodings 

     if($encoding !== false) { 
      $key = array_search($encoding, $encodings) !== false; 
      if ($key !== false) 
       unset($encodings[$key]); 
      $encodings = array_values($encodings); 
     } 

     $encKey = 0; 
     while ($row = fgets($fopen)) { 
      if($encoding == false){ 
       $encoding = $encodings[$encKey++]; 
      } 

      if(!mb_check_encoding($row, $encoding)){ 
       $encoding =false; 
       rewind($fopen); 
      } 

     } 

     return $encoding; 
} 
+0

これまでに何を試しましたか? – Thamaraiselvam

+0

はい、可能です。あなたは何か、Googleのためにそれを試してみましたか? – Qirel

+0

上記のコードを確認してください@Qirel – Dan

答えて

0

のWindows-1254のWindows-1255を指定したPHPのWindows-1251のWindows-1252にあるサポートされていません。

チェック

+0

ありがとうございますが、配列にサポートされているエンコーディングを指定せずに検出する方法はありますか – Dan