YannBergonzat

PHP Detect Encoding

May 20th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.98 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8" />
  5.     <style type="text/css">
  6.         table {
  7.             width: 100%;
  8.             border-collapse: collapse;
  9.         }
  10.  
  11.         th, td {
  12.             border: 1px solid black;
  13.             padding: 10px;
  14.         }
  15.  
  16.         th:nth-of-type(2n), td:nth-of-type(2n) {
  17.             background: rgba(0, 0, 0, 0.1);
  18.         }
  19.  
  20.         tr:nth-of-type(2n) th, tr:nth-of-type(2n) td {
  21.             background: rgba(0, 0, 0, 0.15);
  22.         }
  23.  
  24.         tr:nth-of-type(2n) th:nth-of-type(2n), tr:nth-of-type(2n) td:nth-of-type(2n) {
  25.             background: rgba(0, 0, 0, 0.25);
  26.         }
  27.     </style>
  28. </head>
  29. <body>
  30.     <form method="POST" action="index.php" enctype="multipart/form-data">
  31.         <input type="file" name="file" />
  32.         <button type="submit">Envoyer</button>
  33.     </form>
  34.  
  35.     <?php
  36.         function csvStringToArray($sInput, $sDelimiter=',', $sEnclosure='"') {
  37.             $rTemp = fopen("php://memory", "rw");
  38.             fwrite($rTemp, $sInput);
  39.             fseek($rTemp, 0);
  40.  
  41.             $aReturn = array();
  42.  
  43.             while (($aData = fgetcsv($rTemp, 4096, $sDelimiter, $sEnclosure)) !== false) {
  44.                 $aReturn[] = $aData;
  45.             }
  46.  
  47.             fclose($rTemp);
  48.            
  49.             return $aReturn;
  50.         }
  51.  
  52.         function detectAndConvertEncoding($sInput, &$sOutput) {
  53.             $UTF32_BIG_ENDIAN_BOM = chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF);
  54.             $UTF32_LITTLE_ENDIAN_BOM = chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00);
  55.             $UTF16_BIG_ENDIAN_BOM = chr(0xFE) . chr(0xFF);
  56.             $UTF16_LITTLE_ENDIAN_BOM = chr(0xFF) . chr(0xFE);
  57.             $UTF8_BOM = chr(0xEF) . chr(0xBB) . chr(0xBF);
  58.  
  59.             $aOutputChars = array('...', "'", "'", '"', '"', '•', '-', '-');
  60.             $aUTF8Chars = array("\xe2\x80\xa6", "\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\xa2", "\xe2\x80\x93", "\xe2\x80\x94");
  61.             $aWinChars = array(chr(133), chr(145), chr(146), chr(147), chr(148), chr(149), chr(150), chr(151));
  62.  
  63.             $sFirst2 = substr($sInput, 0, 2);
  64.             $sFirst3 = substr($sInput, 0, 3);
  65.             $sFirst4 = substr($sInput, 0, 3);
  66.  
  67.             $mUTFDetected = false;
  68.  
  69.             if ($sFirst3 == $UTF8_BOM) {
  70.                 $mUTFDetected = 'UTF-8';
  71.             } elseif ($sFirst4 == $UTF32_BIG_ENDIAN_BOM) {
  72.                 $mUTFDetected = 'UTF-32BE';
  73.             } elseif ($sFirst4 == $UTF32_LITTLE_ENDIAN_BOM) {
  74.                 $mUTFDetected = 'UTF-32LE';
  75.             } elseif ($sFirst2 == $UTF16_BIG_ENDIAN_BOM) {
  76.                 $mUTFDetected = 'UTF-16BE';
  77.             } elseif ($sFirst2 == $UTF16_LITTLE_ENDIAN_BOM) {
  78.                 $mUTFDetected = 'UTF-16LE';
  79.             }
  80.  
  81.             $mUTF8Detected = mb_detect_encoding($sInput, 'UTF-8', true);
  82.             $mEncodingDetected = mb_detect_encoding($sInput, 'UTF-8, UTF-16, ISO-8859-1', true);
  83.  
  84.             $bUTF8 = $mUTF8Detected !== false;
  85.             $bCanConvert = $bUTF8 === false;
  86.  
  87.             $sRealEncoding = 'UTF-8';
  88.  
  89.             if ($bUTF8) {
  90.                 if ($mUTFDetected === false) {
  91.                     $sRealEncoding = 'UTF-8 without BOM';
  92.                 }
  93.             } else {
  94.                 if ($mUTFDetected === false) {
  95.                     if ($mEncodingDetected === false) {
  96.                         $sRealEncoding = '[failed]';
  97.                     } else {
  98.                         $sRealEncoding = $mEncodingDetected;
  99.                     }
  100.                 } else {
  101.                     $sRealEncoding = $mUTFDetected;
  102.                 }
  103.             }
  104.  
  105.             if ($bCanConvert) {
  106.                 $sInput = str_replace($aUTF8Chars, $aOutputChars, $sInput);
  107.                 $sInput = str_replace($aWinChars, $aOutputChars, $sInput);
  108.  
  109.                 $sOutput = mb_convert_encoding($sInput, 'UTF-8', $sRealEncoding);
  110.             } else {
  111.                 if ($bUTF8) {
  112.                     $sOutput = $sInput;
  113.                 } else {
  114.                     $sOutput = false;
  115.                 }
  116.             }
  117.  
  118.             return $sRealEncoding;
  119.         }
  120.  
  121.         if (isset($_FILES['file'])) {
  122.             $aFile = $_FILES['file'];
  123.             $sName = $aFile['tmp_name'];
  124.             $sContents = file_get_contents($sName);
  125.             $sEncoding = detectAndConvertEncoding($sContents, $sFixedContents);
  126.            
  127.             echo '<p>Encoding detected: ' . $sEncoding . '</p>';
  128.  
  129.             echo '<table>';
  130.  
  131.             $aData = csvStringToArray($sFixedContents, ';');
  132.  
  133.             for ($i = 0; $i < count($aData); $i++) {
  134.                 echo '<tr>';
  135.  
  136.                 for ($j = 0; $j < count($aData[$i]); $j++) {
  137.                     echo '<td>' . $aData[$i][$j] . '</td>';
  138.                 }
  139.  
  140.                 echo '</tr>';
  141.             }
  142.  
  143.             echo '</table>';
  144.         }
  145.     ?>
  146. </body>
  147. </html>
Advertisement
Add Comment
Please, Sign In to add comment