Skip to content

Instantly share code, notes, and snippets.

@mytory
Last active January 7, 2016 10:43
Show Gist options
  • Save mytory/19599260ef4e2b64c488 to your computer and use it in GitHub Desktop.
Save mytory/19599260ef4e2b64c488 to your computer and use it in GitHub Desktop.
PHP command line script. It detect encoding from files on directory and subdirectories and print it.
<?php
function print_file_encoding_from($dir){
if($handle = opendir($dir)){
echo "\n\n## Directory is $dir\n\n";
while(false !== ($file = readdir($handle))){
if(in_array($file, array('.', '..', '.git'))){
continue;
}
$file_path = realpath($dir . DIRECTORY_SEPARATOR . $file);
$extension = pathinfo($file_path, PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), array('php', 'htm', 'html', 'js', 'txt')) and is_file($file_path)){
continue;
}
if(is_file($file_path)){
$encoding = mb_detect_encoding(file_get_contents($file_path), array('utf-8', 'cp949', 'euc-kr'));
echo "$encoding - $file_path\n";
}else{
print_file_encoding_from($file_path);
}
}
closedir($handle);
}else{
echo 'Opening failed.';
}
}
if(empty($argv[1])){
echo "Usage: php $argv[0] dir_path\n";
exit;
}
$dir = realpath($argv[1]);
if(!$dir){
echo "$argv[1] is incorrect path.";
exit;
}
echo "=== Scanning $dir start! ===\n";
print_file_encoding_from($dir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment