删除Fields PHP中的换行符

时间:2010-03-16 04:41:54

标签: php cakephp

我正在尝试上传excel文件并将其内容存储在Mysql数据库中。

我在保存内容时遇到问题.. 像

我的csv文件采用

的形式
        "1","aruna","IEEE 
         paper"
        "2","nisha","JOurnal magazine"

实际上我有2条记录而我正在使用代码

<?php
  $string = file_get_contents( $_FILES["file"]["tmp_name"] );
  //echo $string;

foreach ( explode( "\n", $string ) as $userString )

    {
                 echo $userString;

            }

&GT?; 因为在Csv记录中,在IEEE和纸张之间插入了一条新行,它将我作为3条记录发送给我。

如何明智地删除这个新行代码并修改代码,以便只考虑记录1和2之间的新行......

3 个答案:

答案 0 :(得分:2)

使用fgetcsv加载解析CSV文件。

答案 1 :(得分:1)

也许您应该使用库来打开和修改Excel文件。试试XLS Reader

答案 2 :(得分:1)

fgetcsv命令功能强大且有用,您应该使用它。但是如果你想要回答原来的问题......

  1. 用一些奇怪的标记替换“好”换行符,例如@EOL @
  2. 用另一个令牌替换“坏”换行符,例如@NL @
  3. 用换行符替换第一个标记
  4. 解析您的数据。
  5. 像这样......

    <?php   
        $txt = <<<_TXT
    "1","aruna","IEEE 
    paper" "2","nisha","JOurnal magazine"
    "2","aruna","IEEE paper" "2","nisha","JOurnal magazine"
    "3","aruna","IEEE paper" "2","nisha","JOurnal
    magazine"
    "4","aruna","IEEE paper" "2","nisha","JOurnal magazine"
    _TXT;
        $txt = str_replace("\"\n", "@EOL@", $txt);
        $txt = str_replace("\n", "@NL@", $txt);
        $txt = str_replace("@EOL@", "\n", $txt);
        echo $txt;
    ?>
    

    输出是......

    “1”,“aruna”,“IEEE @ NL @ paper”“2”,“nisha”,“JOurnal magazine

    “2”,“aruna”,“IEEE论文”“2”,“nisha”,“JOurnal magazine

    “3”,“aruna”,“IEEE paper”“2”,“nisha”,“JOurnal @ NL @ magazine

    “4”,“aruna”,“IEEE论文”“2”,“nisha”,“JOurnal magazine”