1 votes

Comment convertir de nombreux fichiers texte de <some_encoding> en utf8-no-bom ?

Je cherche à convertir de nombreux fichiers texte (plus de 40) de ISO-Latin-1 à UTF8-no-bom. Comment puis-je y parvenir ?

2voto

Vladimir Points 2510

De stackoverflow :

  • You can get a tool such as iconv from GnuWin32 and run a batch script to process all of your files that way.

    But what encoding are they now? If they're ANSI, 
    and you're not using any bytes with values => 128,
    then they're already BOM-less UTF-8.
    Perhaps you can use that to narrow down the number of 
    files you'd have to process - maybe enough that you only have a
    few files to convert (and might prefer to do it on an individual basis).
  • Actually, I do it with Notepad++. Before trying this, you must make a backup of your files. You need to create a macro that does this:

    1.  Convert the currently opened file to UTF-8 w/o BOM;
    2.  Select all the text in your file, and copy it
        (why this? it looks like a bug. if you won't do this,
        your file will be replaced with your current clipboard content...);
    3.  Save the current file;
    4.  Close the current file.
    5.  Save this macro.
    
    Now, open your ~~PHP~~ files, and run it with the "Run a Macro Multiple 
    Times..." command. If you opened 100 files, let it run for 100 times.

-1voto

PmChin Points 1
<?php
$url = getenv("SERVER\_ADDR");
//$url = getenv(HTTP\_POST\_VARS);
$rootdir='d:\\\\xampp\\\\htdocs\\\\ecoder';
$dir="."; 
$files=scan\_dir($rootdir);
  foreach ($files as $file) {
    $info = pathinfo($file);
    $extF =$info\["extension"\];
    if ($extF == "php" || $extF == "txt" || $extF == "js" || $extF == "css") { 
     echo $file ."  
";
     $data = file\_get\_contents($file);
     writeUTF8File($file, $data);
     echo $file.' was converted to UTF8  
';
    }
  }

//use this function to get all files inside a directory (including subdirectories)

function scan\_dir($dir) {
    $arrfiles = array();
    if (is\_dir($dir)) {
        if ($handle = opendir($dir)) {
            chdir($dir);
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    if (is\_dir($file)) {
                        $arr = scan\_Dir($file);
                        foreach ($arr as $value) {
                            $arrfiles\[\] = $dir."/".$value;
                        }
                    } else {
                        $arrfiles\[\] = $dir."/".$file;
                    }
                }
            }
            chdir("../");
        }
        closedir($handle);
    }
    return $arrfiles;
}

function writeUTF8File($filename,$content) { // UTF8
        $f=fopen($filename,"w"); 
        # Now UTF-8 - Add byte order mark 
        fwrite($f, pack("CCC",0xef,0xbb,0xbf)); 
        fwrite($f,$content); 
        fclose($f); 
} 

?>

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X