|
| 1 | +<?php |
| 2 | +namespace Descom\File_Encoding; |
| 3 | + |
| 4 | +class FileEncoding |
| 5 | +{ |
| 6 | + /** |
| 7 | + * Encode file. |
| 8 | + * |
| 9 | + * @param string $fileR Original file |
| 10 | + * @param string &$encoding_to Encoding to encode file |
| 11 | + * @param string &$encodings_detected Ordered list of encodings |
| 12 | + * |
| 13 | + * @return bool |
| 14 | + */ |
| 15 | + public function encodeFile($file, $encoding_to = 'UTF-8', $encodings_detected = 'UTF-8,ISO-8859-1,WINDOWS-1252') |
| 16 | + { |
| 17 | + $encoding_original = $this->detectEncoding($file, $encodings_detected); |
| 18 | + if ($encoding_original != $encoding_to) { |
| 19 | + try { |
| 20 | + $fileW = '.temp'; |
| 21 | + $handleR = @fopen($file, 'r'); |
| 22 | + $handleW = @fopen($fileW, 'w'); |
| 23 | + if ($handleR && $handleW) { |
| 24 | + while ($line = fgets($handleR, 4096)) { |
| 25 | + fwrite($handleW, mb_convert_encoding($line, $encoding_to, $encoding_original), 4096); |
| 26 | + } |
| 27 | + fclose($handleR); |
| 28 | + fclose($handleW); |
| 29 | + unlink($file); |
| 30 | + rename($fileW, $file); |
| 31 | + |
| 32 | + } |
| 33 | + else{ |
| 34 | + fclose($handleR); |
| 35 | + fclose($handleW); |
| 36 | + return false; |
| 37 | + } |
| 38 | + } catch (Exception $e) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + } |
| 42 | + return $this->checkEncoding($file, $encoding_to); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Detect file encoding. |
| 47 | + * |
| 48 | + * @param string $fileR Original file |
| 49 | + * @param string $encodings_detected Ordered list of encodings |
| 50 | + * |
| 51 | + * @return string File encoding |
| 52 | + */ |
| 53 | + public function detectEncoding($fileR, $encodings_detected) |
| 54 | + { |
| 55 | + $encodings = explode(',', $encodings_detected); |
| 56 | + $handleR = @fopen($fileR, 'r'); |
| 57 | + if ($handleR && count($encodings)) { |
| 58 | + while ($line = fgets($handleR, 4096)) { |
| 59 | + $encoding = mb_detect_encoding($line, $encodings_detected, true); |
| 60 | + if ($encoding != $encodings[0]) { |
| 61 | + fclose($handleR); |
| 62 | + return $encoding; |
| 63 | + } |
| 64 | + } |
| 65 | + fclose($handleR); |
| 66 | + return $encodings[0]; |
| 67 | + } |
| 68 | + else |
| 69 | + return ''; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Check if file encoding matches encoding_to. |
| 74 | + * |
| 75 | + * @param string $fileR Original file |
| 76 | + * |
| 77 | + * @return bool |
| 78 | + */ |
| 79 | + public function checkEncoding($fileR, $encoding_to) |
| 80 | + { |
| 81 | + $handleR = @fopen($fileR, 'r'); |
| 82 | + |
| 83 | + if ($handleR) { |
| 84 | + while ($line = fgets($handleR, 4096)) { |
| 85 | + if (!mb_check_encoding($line, $encoding_to)) { |
| 86 | + fclose($handleR); |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + fclose($handleR); |
| 91 | + } |
| 92 | + else |
| 93 | + return false; |
| 94 | + |
| 95 | + return true; |
| 96 | + } |
| 97 | +} |
0 commit comments