php忽略一些字符的新行fwrite csv

时间:2015-02-25 06:55:32

标签: php csv fwrite

这是csv文件:

KOL2    3   KOLKA05 BLTLA1  NA  S17C2   1_5_3   8_20_2  8   NA  NA  NA  NA  23:958447298
KOL2    3   KOLKA05 BLTLA1  NA  S17C2   1_4_3   3_57_2  7   NA  NA  NA  NA  23:958447297
KOL2    3   KOLKA05 BLTLA1  NA  S17C2   1_1_2   8_20_1  6   NA  NA  NA  NA  23:958447296
KOL2    3   KOLKA05 BLTLA1  NA  S15C1   1_4_3   7_19_1  0   NA  NA  NA  0   23:958407892
MUM4    7   BHOPA04 BZIV01  NA  S15C2   2_5_3   20_36_0 0   7684    5402    0   23:958426337    3.794444444
MUM5    9   MUMBA15 BUIP04  NA  S15C3   3_2_3   30_38_2 2   7712    10730   1   4.455555556 10:73339
MUM5    15  MUMBA14 BUIP04  NA  S15C1   3_5_1   16_42_0 0   7712    10730   0   23:46542    ***Pending

我试图在用户的特定搜索和csv文件中的fwrite之后从csv获取值

$f = fopen("test.csv", "r");
$to_write = fopen("sample.csv", "w");
while (($line = fgetcsv($f)) !== false)
{
foreach($line as $new)
{ 
if($new!="")
    {
      echo "<td>".$new."</td>";

       $new=str_replace("-","NA",$new);

         if($new == $line[13]) 
         {
          fwrite($to_write,$new.","."\n");
         }
          else 
         {
          fwrite($to_write,$new.",");
         }
    }
}   
}

对于每第13列,我在csv的下一行写了一行,但是对于一些特殊的值,例如&#39; 0&#39;,&#39; - &#39; ...来自于test.csv文件......

数据写在下一行......

在sample.csv文件中完成输出写入之后: 我得到这样的输出:

KOL2    3   KOLKA05 BLTLA1  NA  S17C2   1_5_3   8_20_2  8   NA  NA  NA  NA  23:958447298
KOL2    3   KOLKA05 BLTLA1  NA  S17C2   1_4_3   3_57_2  -                   
NA  NA  NA  NA  23:958447297                                    
KOL2    3   KOLKA05 BLTLA1  NA  S17C2   1_1_2   8_20_1  6   NA  NA  NA  NA  23:958447296
KOL2    3   KOLKA05 BLTLA1  NA  S15C1   1_4_3   7_19_1  0                   
NA  NA  NA  0   23:958407892                                    
MUM4    7   BHOPA04 BZIV01  NA  S15C2   2_5_3   20_36_0 0                   
7684    5402    0   23:958426337    3.794444444                                 
MUM5    9   MUMBA15 BUIP04  NA  S15C3   3_2_3   30_38_2 2   7712    10730   1   4.455555556 10:73339
MUM5    15  MUMBA14 BUIP04  NA  S15C1   3_5_1   16_42_0 0                   
7712    10730   0   23:46542    ***Pending  

我正在努力弄清楚它为什么会发生,对于特定情况的&#39; - &#39;,&#39; 0&#39; ....文件正在写下一个row ...如何忽略像这样的特定值的下一行......

0 个答案:

没有答案