私がやっていることは、アーカイブされたCSVデータを変換することです。それは数千のファイルでうまくいきました。私は日付を解析し、それをタイムスタンプに変換します。しかし、1つのファイルでは、何とか動作しません。私は(int) $string
を使用して、解析された文字列をint値にキャストします.->int(0)
を返します。私もintval()
- >同じ結果を使用しました。 var_dump($string)
を使用すると、奇妙な出力が得られます。たとえば、string(9) "2008"
です。これは実際にはstring(4) "2008"
です。文字列にpreg_match
を使用しようとしましたが、成功しませんでした。これはエンコードの問題ですか?ここでPHP解析/型キャストの問題
date_default_timezone_set('UTC');
$ms = 0;
function convert_csv($filename)
{
$target = "tmp.csv";
$fp = fopen("$filename","r") or die("Can't read the file!");
$fpo = fopen("$target","w") or die("Can't read the file!");
while($line = fgets($fp,1024))
{
$linearr = explode(",","$line");
$time = $linearr[2];
$bid = $linearr[3];
$ask = $linearr[4];
$time = explode(" ",$time);
$date = explode("-",$time[0]);
$year = (int) $date[0]);
$month = (int)$date[1];
$day = (int)$date[2];
$time = explode(":",$time[1]);
$hour = (int)$time[0];
$minute = (int)$time[1];
$second = (int)$time[2];
$time = mktime($hour,$minute,$second,$month,$day,$year);
if($ms >= 9)
{
$ms = 0;
}else
{
$ms ++;
}
$time = $time.'00'.$ms;
$newline = "$time,$ask,$bid,0,0\n";
fwrite($fpo,$newline);
}
fclose($fp);
fclose($fpo);
unlink($filename);
rename($target,$filename);
}
私たちが話しているファイルへのリンクです:
ここはいくつかのコードで、それだけでかなり標準のものです
いくつかのコードを表示してください。また 'string(9)" 2008 "'を得ましたか? – Dogbert
文字列のヘキサ・ダンプは確かに良いアイデアです。一見高すぎる文字列の長さは、そこに出力ビューアが表示できない、または表示されないバイトがあることを示します。 –
あなたのコメントをありがとう、私はちょうどコードを追加! – user871784