在打开之前替换csv中的线条

时间:2013-08-07 11:03:49

标签: php csv fgetcsv

我有一个csv,我通过fopen读,然后是fgetcsv:

ini_set('auto_detect_line_endings', true);
$handle = fopen($uploadDir.$fileName,"r");
while (($aCell = fgetcsv($handle, 1000, ";")) !== FALSE) {
    //GO THROUGH ROWS
} 

我的问题是来自windows的csv将CRLF(\ r \ n)或LF(\ n)作为换行符。有时我有一个来自mac的csv,它使用CRLF,有时使用CR(主要是带有st.html文本的文本列)

CR破坏了fgetcsv,因此我没有像“已定义”标题列中那样获得正确的列和行结构,而是使用了windoof。

如何在通过线路之前用csv中的LF(\ n)替换CR(\ r)?

修改 我试过Passerbys Idea它有效。 Thx for your tips

            $file_contents = file_get_contents($uploadDir.$fileName);
            $file_cont_tmp = str_replace("\r","\r\n",$file_contents);
            $file_cont_new = str_replace("\r\n\n","\r\n",$file_cont_tmp);
            file_put_contents($uploadDir.$fileName,$file_cont_new);

0 个答案:

没有答案
相关问题