简体   繁体   English

将行写入 .csv 或 .txt 文件 php 的限制

[英]limitation on writing line to .csv or .txt file php

i wrote a convert bulk data from an excel to a .txt or .csv by using fwrite php.我使用 fwrite php 编写了将批量数据从 excel 转换为 .txt 或 .csv 的方法。 however when I fread on the .txt file that my data placed on, the data turned out broken line.但是,当我查看我的数据所在的 .txt 文件时,数据变成了断线。 how can i fix it?我该如何解决? Attached here is the highlighted from .txt or .csv and also the codes as below:-此处附加的是 .txt 或 .csv 中突出显示的内容以及以下代码:-

**********my write code ************* **********我写的代码**************

for loop here {
    $split_filename_path = fopen($tempfile_path.$split_filename.$total_round.$extension, "w");`
    $split_file_txt = $ISBN.$comma.$Title.$comma.$Qty.$comma.$Location.$comma.$outlet;
    fwrite($split_filename_path, $split_file_txt);
}
fclose($split_filename_path);

then it comes here my read code *****然后它来到这里我的阅读代码*****

    $myfile = fopen($file, "r");
    $fread = fread($myfile,filesize($file));
        
    fclose($myfile);
    $split = explode("\n",$fread);
    $datafields = array('ISBN', 'title', 'qty', 'location', 'outlet');
    $insertvalues = array();
    foreach($split as $string){ 
        $row = explode(",",$string);
        $questionmarks[] = '(' . $this->placeholder('?',$irow, sizeof($row)) . ')'; 
        $insertvalues = array_merge( $insertvalues, array_values($row));
    }
    $sql = "INSERT INTO temp_data (".implode( ',', $datafields) . ") 
                VALUES ". implode( ',', $questionmarks);
    
    function placeholder( $text, $irow, $count = 0, $separator = ',' ) {
        $result = array();
        if ($count > 0) {
            for ($x = 0; $x < $count; $x++) {
                $result[] = $text;
            }
        }
        return implode($separator, $result);
    }

with this codes, the result that I have is here :使用此代码,我得到的结果在这里:

9780794435295,BARBIE & HER SISTERS IN THE GREAT PUPPY ADVENTURE: (supposed to be full :- BARBIE & HER SISTERS IN THE GREAT PUPPY ADVENTURE: A SLIDING TAB BOOK BARBIE MOVIE TIEIN )

在此处输入图像描述

found the answer.找到了答案。 $Title = preg_replace( "/\r|\n/", "", $Title); $Title = preg_replace("/\r|\n/", "", $Title);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM