【发布时间】:2012-03-31 22:37:25
【问题描述】:
我要做的是转换一些存档的 CSV 数据。它在几千个文件上运行良好。我解析出日期并将其转换为时间戳。但是在一个文件上,不知何故它不起作用。我使用(int) $string 将解析后的字符串转换为int 值-> 它返回int(0)。我还使用了intval() -> 相同的结果。当我使用var_dump($string) 时,会得到一些奇怪的输出,例如string(9) "2008",实际上应该是string(4) "2008"。我试图在字符串上使用preg_match,但没有成功。这是编码问题吗?
这是一些代码,只是非常标准的东西:
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"? -
字符串的十六进制转储肯定是个好主意,因为看似太长的字符串长度表明其中有一些字节您的输出查看器不能或不会显示。
-
感谢您的 cmets,我刚刚添加了代码!