Skip to content

Instantly share code, notes, and snippets.

@kurko
Created October 16, 2009 17:28
Show Gist options
  • Save kurko/211900 to your computer and use it in GitHub Desktop.
Save kurko/211900 to your computer and use it in GitHub Desktop.
Tranforme todos os arquivos de um diretório e suas subpastas de ISO-8859-1 para UTF-8. Cuidado: não rode este script em arquivo que não sejam ISO-8859-1, senão você vai perder seus arquivos. Para se precaver contra isto, é necessário implementar uma verif
<?php
/*
* NÃO USE ESTE SCRIPT SOBRE ARQUIVOS UTF-8! VOCẼ PERDERÁ TUDO.
*
* Não use este arquivo sem fazer Backup dos arquivos antes.
*
* Alterações necessárias:
* - verificar se o arquivo atual é ISO-8859-1 realmente.
* - verificar se é um arquivo de texto (transformar imagens vai inutilizá-las
*/
function fileExtension($param){
$ext = explode('.',$param);
$ext = array_reverse($ext);
return $ext[0];
}
function utf($u, $outputDirs = true, $outputFiles = false){
$status["filesDone"] = 0;
$status["dirsFound"] = 0;
foreach( glob($u) as $valor ){
if( is_dir($valor) ){
if( $outputDirs )
print $valor."\n";
$status["dirsFound"]++;
$statusTmp = utf($valor."/*", $outputDirs, $outputFiles);
$status["dirsFound"]+= $statusTmp["dirsFound"];
$status["filesDone"]+= $statusTmp["filesDone"];
} else {
/**
* @todo - Verify if it's really ASCII
*/
/*
* Don't do anything on files having the extensions below
*/
if( !in_array(
fileExtension($valor),
array( "jpg",
"png",
"gif",
"swf",
"mov",
"wav",
"mov",
"mp3",
"wma",
"bmp",
"svg"
)
)
)
{
exec("iconv -f iso-8859-1 -t utf-8 $valor > $valor.utf");
exec("mv $valor.utf $valor");
}
$status["filesDone"]++;
if( $outputFiles ){
print "\t".$valor;
print "\n";
}
}
}
return $status;
}
$status = utf("*", false);
print "Transformação concluída.\n";
print "\tDiretório encontrados: ".$status["dirsFound"]."\n";
print "\tArquivos modificados: ".$status["filesDone"]."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment