问题
I have bunch of files that are not in UTF-8 encoding and I\'m converting a site to UTF-8 encoding.
I\'m using simple script for files that I want to save in utf-8, but the files are saved in old encoding:
header(\'Content-type: text/html; charset=utf-8\');
mb_internal_encoding(\'UTF-8\');
$fpath=\"folder\";
$d=dir($fpath);
while (False !== ($a = $d->read()))
{
if ($a != \'.\' and $a != \'..\')
{
$npath=$fpath.\'/\'.$a;
$data=file_get_contents($npath);
file_put_contents(\'tempfolder/\'.$a, $data);
}
}
How can I save files in utf-8 encoding?
回答1:
file_get_contents / file_put_contents will not magically convert encoding.
You have to convert the string explicitly; for example with iconv() or mb_convert_encoding().
Try this:
$data = file_get_contents($npath);
$data = mb_convert_encoding($data, 'UTF-8', 'OLD-ENCODING');
file_put_contents('tempfolder/'.$a, $data);
Or alternatively, with PHP's stream filters:
$fd = fopen($file, 'r');
stream_filter_append($fd, 'convert.iconv.UTF-8/OLD-ENCODING');
stream_copy_to_stream($fd, fopen($output, 'w'));
回答2:
Add BOM: UTF-8
file_put_contents($myFile, "\xEF\xBB\xBF". $content);
回答3:
<?php function writeUTF8File($filename,$content) { $f=fopen($filename,"w"); # Now UTF-8 - Add byte order mark fwrite($f, pack("CCC",0xef,0xbb,0xbf)); fwrite($f,$content); fclose($f); } ?>
回答4:
Iconv to the rescue.
回答5:
On Unix/Linux a simple shell command could be used alternatively to convert all files from a given directory:
recode L1..UTF8 dir/*
Could be started via PHPs exec() as well.
回答6:
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
I got this line from Cool
回答7:
If you want to use recode recursively, and filter for type, try this:
find . -name "*.html" -exec recode L1..UTF8 {} \;
回答8:
This works for me. :)
$f=fopen($filename,"w");
# Now UTF-8 - Add byte order mark
fwrite($f, pack("CCC",0xef,0xbb,0xbf));
fwrite($f,$content);
fclose($f);
回答9:
I put all together and got easy way to convert ANSI text files to "UTF-8 No Mark":
function filesToUTF8($searchdir,$convdir,$filetypes) {
$get_files = glob($searchdir.'*{'.$filetypes.'}', GLOB_BRACE);
foreach($get_files as $file) {
$expl_path = explode('/',$file);
$filename = end($expl_path);
$get_file_content = file_get_contents($file);
$new_file_content = iconv(mb_detect_encoding($get_file_content, mb_detect_order(), true), "UTF-8", $get_file_content);
$put_new_file = file_put_contents($convdir.$filename,$new_file_content);
}
}
Usage: filesToUTF8('C:/Temp/','C:/Temp/conv_files/','php,txt');
回答10:
- Open your files in windows notebook
- Change the encoding to be an UTF-8 encoding
- Save your file
- Try again! :O)
来源:https://stackoverflow.com/questions/4839402/how-to-write-file-in-utf-8-format