Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用fputcsv()将数据正确添加到csv文件中_Php_Regex_Excel_Csv_Fputcsv - Fatal编程技术网

Php 使用fputcsv()将数据正确添加到csv文件中

Php 使用fputcsv()将数据正确添加到csv文件中,php,regex,excel,csv,fputcsv,Php,Regex,Excel,Csv,Fputcsv,我想将curl数据正确地添加到csv文件中, 当我试图将数据添加到文件中时,数据被全部添加到csv文件中 我只想将其添加到单元格中 以下是我的密码- <?php define('CSV_PATH','csvfiles/'); $csv_file = CSV_PATH . "file_input_url.csv"; // Name of your input file $csvfile = fopen($csv_file, 'r'); $csv_file

我想将curl数据正确地添加到csv文件中, 当我试图将数据添加到文件中时,数据被全部添加到csv文件中

我只想将其添加到
单元格中

以下是我的密码-

<?php

    define('CSV_PATH','csvfiles/');

    $csv_file = CSV_PATH . "file_input_url.csv"; // Name of your input file
    $csvfile = fopen($csv_file, 'r');

    $csv_fileoutput = CSV_PATH . "file_output_content.csv"; // Name of output file
    $csvfileoutput = fopen($csv_fileoutput, 'a');

    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename=' . $csvfileoutput);
    header("Content-Transfer-Encoding: UTF-8");

    $headers = ['URL','Code_content'];

    fputcsv($csvfileoutput, $headers);
    function cleanData(&$str)
              {
                // escape tab characters
                $str = preg_replace("/\t/", "\\t", $str);

                // escape new lines
                $str = preg_replace("/\r?\n/", "\\n", $str);

                // convert 't' and 'f' to boolean values
                if($str == 't') $str = 'TRUE';
                if($str == 'f') $str = 'FALSE';

                // force certain number/date formats to be imported as strings
                if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
                  $str = "'$str";
                }

                // escape fields that include double quotes
                if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
              }

while($data = fgetcsv($csvfile)) 
    {

        $url = $data[0];
    //  $url = "http://www.partsimple.com/";

        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POST, true);  
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $str = curl_exec ($ch); 
        curl_close ($ch); 

        cleanData($str);

        fputcsv($csvfileoutput,array($url,$str));

    }

?>

更新 这是我用这个代码得到的结果。我希望所有这些数据都在一个单元格中

如url和下一个单元格中的内容

我还尝试正确格式化数据。但它并没有像预期的那样工作

请给我一些指导


谢谢

将函数cleanData()更改如下-

function cleanData(&$str)
        {
            //$str = preg_replace("/\t/", "\\t", $str);

            $str = str_replace(array("\n\r", "\n", "\r"), '', $str);

            $str = trim(preg_replace('/\t+/', '', $str));

            $str = preg_replace('/\s+/S', " ", $str);

        }