title: How to detect the proper encoding in PHP? mb_detect_encoding isn't working
body: <p>I need to be able to detect a string's encoding but mb_detect_encoding isn't working.</p>
  
  <p>I obtain the string from a file (<code>file_get_contents</code>) and I know the file that was giving me trouble was in UTF-16 LE. However, from the docs what I understand is that detecting this encoding is not possible (<a href="http://php.net/manual/en/function.mb-detect-order.php" rel="nofollow">mb_detect_order </a>: "For UTF-16, UTF-32, UCS2 and UCS4, encoding detection will fail always.").</p>
  
  <p>How can I obtain a string's encoding in a trustworthy way in PHP? Any possible encoding?</p>
  
  <p>I lost multiple hours trying to solve this but I found no good resource. I would like to be able to automate this so if my file changes its encoding, my program will be able to handle it (I am obtaining the file from another website).</p>
  
  <hr>
  
  <p>I've tried this with no success, it tells me UTF-8:</p>
  
  <pre><code>mb_detect_encoding($proper_string, 'UTF-16LE,UCS-2,UTF-8,ASCII', true)
  </code></pre>
  
  <p>I've also tried this:</p>
  
  <pre><code>echo 'mb_check_encoding($fileContents, \'UTF-8\'): ' . mb_check_encoding($fileContents, 'UTF-8') . "\n";
  //true
  echo 'mb_check_encoding($fileContents, \'UTF-16\'): ' . mb_check_encoding($fileContents, 'UTF-16') . "\n";
  //true
  echo 'mb_check_encoding($fileContents, \'UTF-16LE\'): ' . mb_check_encoding($fileContents, 'UTF-16LE') . "\n";
  //true
  echo 'mb_check_encoding($fileContents, \'UCS-2\'): ' . mb_check_encoding($fileContents, 'UCS-2') . "\n";
  //true
  echo 'mb_check_encoding($fileContents, \'ISO-8859-1\'): ' . mb_check_encoding($fileContents, 'ISO-8859-1') . "\n";
  //true
  </code></pre>