Skip to content

Instantly share code, notes, and snippets.

@rogeriopradoj
Last active August 29, 2015 14:05
Show Gist options
  • Save rogeriopradoj/a40d19f8969abe8697c9 to your computer and use it in GitHub Desktop.
Save rogeriopradoj/a40d19f8969abe8697c9 to your computer and use it in GitHub Desktop.
utf8.php

Transform textfiles from any encoding type to utf8

wget -O utf8.php http://goo.gl/QrblHb
chmod +x utf8.php
./utf8.php

Example, for all srt in a folder:

for f in *.srt; do ./utf8.php $f; done
#!/usr/bin/env php
<?php
$file = isset($argv[1]) ? $argv[1]: '';
if (! is_file($file)) {
echo 'ERROR: Not a file' . PHP_EOL;
exit(1);
}
$commandGetOriginalEncoding = 'file -b --mime-encoding "' . $file . '"';
$originalEncoding = exec($commandGetOriginalEncoding);
$commandSetUtf8 = 'iconv -f ' . $originalEncoding . ' -t utf8 "' . $file . '" > "_' . $file . '"';
exec($commandSetUtf8);
$commandGetFinalEncoding = 'file -b --mime-encoding "_' . $file . '"';
$finalEncoding = exec($commandGetFinalEncoding);
echo PHP_EOL;
echo 'DONE' . PHP_EOL;
echo PHP_EOL;
echo 'Final Encoding: ' . $finalEncoding . PHP_EOL;
echo 'Final file: _' . $file . PHP_EOL;
echo 'Original Encoding: ' . $originalEncoding . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment