Skip to content

Instantly share code, notes, and snippets.

@categulario
Created June 20, 2014 17:44
Show Gist options
  • Select an option

  • Save categulario/c986dc051a8a44ce3afa to your computer and use it in GitHub Desktop.

Select an option

Save categulario/c986dc051a8a44ce3afa to your computer and use it in GitHub Desktop.
php change file encoding
<?php
$filename = "changeme.csv";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
if (!mb_check_encoding($contents, 'UTF-8')
OR !($contents === mb_convert_encoding(mb_convert_encoding($contents, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {
$contents = mb_convert_encoding($contents, 'UTF-8');
}
$fp = fopen('changed.csv', 'w');
fwrite($fp, $contents);;
fclose($fp);
echo 'done!'.PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment