Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Created October 10, 2014 20:18
Show Gist options
  • Save wesleybliss/6fde9dc5432ca583a30e to your computer and use it in GitHub Desktop.
Save wesleybliss/6fde9dc5432ca583a30e to your computer and use it in GitHub Desktop.
Example of converting files in a directory from UTF-8 to ASCII
<?php
print PHP_EOL;
if ( count($argv) < 2 ) {
exit(
'Convert all files in a directory from UTF-8 to ANSI' .
PHP_EOL . 'USAGE: ' . $argv[0] . ' <path>' . PHP_EOL
);
}
$path = $argv[1];
if ( !is_dir($path) ) {
exit( 'Not a directory - ' . $path );
}
$dh = @opendir( $path );
if ( !$dh ) {
exit( 'Couldn\'t open path - ' . $path );
}
print 'Converting files...' . PHP_EOL;
while ( ($file = readdir($dh)) !== false ) {
if ( in_array( $file, array('.', '..', $argv[0])) ) {
// Skip system "previous/current" directories, and self
continue;
}
print ' > ' . $file . PHP_EOL;
$fullPath = ( $path . '/' . $file );
$data = file_get_contents( $fullPath );
$converted = mb_convert_encoding( $data, 'UTF-8', 'ASCII' );
file_put_contents( $fullPath, $converted, LOCK_EX );
}
print PHP_EOL;
@closedir( $dh );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment