Skip to content

Instantly share code, notes, and snippets.

@mytory
Last active December 20, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mytory/6199350 to your computer and use it in GitHub Desktop.
Save mytory/6199350 to your computer and use it in GitHub Desktop.
Convert file encoding with `//IGNORE` flag. This script convert a file, can`t convert files. usage: php iconv.php from-encoding to-encoding filename
<?php
// filename : iconv.php
if($argc != 4){
echo "invalid arg!\nsyntax) php {$argv[0]} from to filename\nex) php {$argv[0]} euc-kr utf-8 /path/to/filename\n";
exit(0);
}
$from = $argv[1];
$to = $argv[2];
$filename = $argv[3];
$pathinfo = pathinfo(realpath($filename));
$newpath = $pathinfo['dirname'] . DIRECTORY_SEPARATOR . $pathinfo['filename'] . '('. $to .').' . $pathinfo['extension'];
$fp = fopen($filename, "rb") or die("fopen failed");
$fp2 = fopen($newpath, 'w');
while(!feof($fp)) {
$string = fgets($fp);
$replaced = iconv($from, $to . '//IGNORE', $string);
fwrite($fp2, $replaced);
}
fclose($fp2) or die("fclose failed");
fclose($fp) or die("fclose failed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment