Jak zapisać plik w formacie UTF-8?
Mam kilka plików, które nie są w kodowaniu UTF-8 i konwertuję stronę do kodowania UTF-8.
Używam prostego skryptu dla plików, które chcę zapisać w utf-8, ale pliki są zapisywane w starym kodowaniu:
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);
}
}
Jak mogę zapisać pliki w kodowaniu utf-8?
10 answers
File_get_contents / file_put_contents nie konwertuje kodowania w magiczny sposób.
Musisz przekonwertować łańcuch jawnie; na przykład z iconv()
lub mb_convert_encoding()
.
Spróbuj tego:
$data = file_get_contents($npath);
$data = mb_convert_encoding($data, 'UTF-8', 'OLD-ENCODING');
file_put_contents('tempfolder/'.$a, $data);
Lub alternatywnie, z filtrami strumienia PHP:
$fd = fopen($file, 'r');
stream_filter_append($fd, 'convert.iconv.UTF-8/OLD-ENCODING');
stream_copy_to_stream($fd, fopen($output, 'w'));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-29 22:47:06
Dodaj BOM: UTF-8
file_put_contents($myFile, "\xEF\xBB\xBF". $content);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-15 15:16:31
<?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); } ?>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-28 21:47:36
Iconv na ratunek.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-29 21:09:21
Na Unix/Linux proste polecenie powłoki może być użyte alternatywnie do konwersji wszystkich plików z danego katalogu:
recode L1..UTF8 dir/*
Można również uruchomić za pomocą PHPS exec ().
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-30 06:01:48
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
Mam ten tekst z Cool
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-26 16:18:34
Jeśli chcesz użyć rekurencyjnie recode i filtrować dla type, spróbuj tego:
find . -name "*.html" -exec recode L1..UTF8 {} \;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-12 16:17:42
To mi pasuje. :)
$f=fopen($filename,"w");
# Now UTF-8 - Add byte order mark
fwrite($f, pack("CCC",0xef,0xbb,0xbf));
fwrite($f,$content);
fclose($f);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-30 11:28:44
Poskładałem wszystko do kupy i dostałem łatwy sposób na konwersję plików tekstowych ANSI do "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');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-05 12:26:37
- Otwieranie plików w notatniku windows
- Zmień kodowanie na kodowanie UTF-8
- Zapisz swój plik Spróbuj jeszcze raz! : O)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-28 11:33:24