UTF-8の課題を読みながらCSVファイルfgetcsv
質問
私は読み込み、CSV、エコーの内容です。そのコンテンツ表示の文字が間違っている。
Mäx Müstermänn->MÃ野¤x Müstermänn
エンコードをCSVファイルはUTF-8BOM(確認Notepad++).
このコンテンツのCSVファイル:
"Mäx";"Müstermänn"
私のPHPスクリプト
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$handle = fopen ("specialchars.csv","r");
echo '<table border="1"><tr><td>First name</td><td>Last name</td></tr><tr>';
while ($data = fgetcsv ($handle, 1000, ";")) {
$num = count ($data);
for ($c=0; $c < $num; $c++) {
// output data
echo "<td>$data[$c]</td>";
}
echo "</tr><tr>";
}
?>
</body>
</html>
私は利用しようとした setlocale(LC_ALL, 'de_DE.utf8');
示唆されるように こちらの せずに成功。のコンテンツは誤表示されます。
また僕に足り?
編集:
An echo mb_detect_encoding($data[$c],'UTF-8');
私はUTF-8UTF-8です。
echo file_get_contents("specialchars.csv");
与えてください "Mäx";"Müstermänn"
.
や
print_r(str_getcsv(reset(explode("\n", file_get_contents("specialchars.csv"))), ';'))
与えてください
Array ( [0] => Mäx [1] => Müstermänn )
はどのような意味でしょうか?
解決 6
現在わかった作業を除去した後、 header
を出力します。と思い、このエンコードのphpファイルがISO-8859-1に示します。設定しますのでUTF-8なBOM.っているが、恐らく私の追加-元に戻す.
また、使用した SET NAMES 'utf8'
のデータベースです。今でも正しいデータベースです。
他のヒント
してみてください:
<?php
$handle = fopen ("specialchars.csv","r");
echo '<table border="1"><tr><td>First name</td><td>Last name</td></tr><tr>';
while ($data = fgetcsv ($handle, 1000, ";")) {
$data = array_map("utf8_encode", $data); //added
$num = count ($data);
for ($c=0; $c < $num; $c++) {
// output data
echo "<td>$data[$c]</td>";
}
echo "</tr><tr>";
}
?>
た同様の問題:構文解析CSVファイルの特殊文字のようにé,è,ö。
以下の手私にとって:
を代表する文字が正しいhtmlのページのヘッダが必要であった:
header('Content-Type: text/html; charset=UTF-8');
するための構文解析べての文字を正しく使用した:
utf8_encode(fgets($file));
ぜっており、すべての以下の文字列操作に'マルチバイト文字列関数'のように:
mb_strtolower($value, 'UTF-8');
うとして位置付け、これ上部のファイルの出力):
<?php
header('Content-Type: text/html; charset=UTF-8');
?>
この機能を返しますUTF-8となります(チェックをmb_detect_encoding) に変換しないで, は、これらの文字がUTF-8です。このため、必要に逆変換を初期エンコーディング(窓-1251はCP1251) iconv.ですが、fgetcsv配列を返しますであることを書くのカスタム関数:[にたどり着きました。]
function customfgetcsv(&$handle, $length, $separator = ';'){
if (($buffer = fgets($handle, $length)) !== false) {
return explode($separator, iconv("CP1251", "UTF-8", $buffer));
}
return false;
}
私の場合、ソースファイルエントランス部分はコンパクト-1250エンコーディングとiconv版画トンのお知らせしますので不正な文字入力文字列...
この溶液に助かったいくつかの
/**
* getting CSV array with UTF-8 encoding
*
* @param resource &$handle
* @param integer $length
* @param string $separator
*
* @return array|false
*/
private function fgetcsvUTF8(&$handle, $length, $separator = ';')
{
if (($buffer = fgets($handle, $length)) !== false)
{
$buffer = $this->autoUTF($buffer);
return str_getcsv($buffer, $separator);
}
return false;
}
/**
* automatic convertion windows-1250 and iso-8859-2 info utf-8 string
*
* @param string $s
*
* @return string
*/
private function autoUTF($s)
{
// detect UTF-8
if (preg_match('#[\x80-\x{1FF}\x{2000}-\x{3FFF}]#u', $s))
return $s;
// detect WINDOWS-1250
if (preg_match('#[\x7F-\x9F\xBC]#', $s))
return iconv('WINDOWS-1250', 'UTF-8', $s);
// assume ISO-8859-2
return iconv('ISO-8859-2', 'UTF-8', $s);
}
対@manvelの解答用str_getcsvでは爆発のためこのような場合:
some;nice;value;"and;here;comes;combinated;value";and;some;others
爆発と爆発す文字列をパート
some
nice
value
"and
here
comes
combinated
value"
and
some
others
がstr_getcsvと爆発す文字列をパート
some
nice
value
and;here;comes;combinated;value
and
some
others