1

I want to add curl data properly into csv file, When I tried to add data into file data is added all over into csv file ..

I want to add it in only cell

Below is my code -

<?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));

    }

?>

Update

This is the csv file I am getting with this code. I want all this data in one cell.

like url and into next cell their content.

I tried this tutorial also to properly format data. But its not working as expect.

Please give some guidance.

Thanks

1 Answer 1

2

Change your function cleanData() as follows -

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);

        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.