3

So I am trying to use fputcsv, and it works, almost like it is intented. I have this weird problem, that I can not seem to solve, and I do not find any good documentation or even people who have this problem.

When there is a string that is being put in the csv, it goes well most of the time, but sometime it starts a new line in the middle of the string for no reason, when I go into the MySQL DB it does not show any stuff like \n, of even a weird character.

It just starts a new line for no reason, what I see sometimes is that there is a "long" word written like this: "today is saturday.tomorrow is sunday" and then it starts a new line after tomorrow, there is no space between the period and next word, but this does not seem a good reason to start a new line?

When I print out the array, it also has the same line ending at that spot... Anyone have any idea what it could be?

My apologies if this does not make any sense...

EDIT: added code and examples

public function actionExportCSV()
{
    $model = Vicreg::model()->findAllByAttributes(array('event_id' => $_GET['eventid']));
    if($model){
        $output = fopen("php://output",'w') or die("Can't open php://output");
        header("Content-Type:application/csv"); 
        header("Content-Disposition:attachment;filename=verzorgingen.csv"); 
        fputcsv($output, array('id','event_id','name','firstname','dob','sex','urgency','pathology','pathology_other','treatment','treatment_other','medication','medication_other','material_other','material','docs','hour_in','hour_out','station_id','nurses_id','transport_id','hospital_id','ambulance_id','closed'));
        foreach($model as $vicreg) {
            $array = array();
            //if($vicreg->id == "83") { print_r($vicreg); exit(0); }
            foreach($vicreg->attributes as $key => $attribute) {
                $attribute = str_replace('  ', '', $attribute);
                $array[$key] = trim(stripslashes($attribute));
            }
            fputcsv($output, $array, ',' ,'"');
        }

        fclose($output) or die("Can't close php://output");
    } else {
        throw new CHttpException(422, 'Geen evenement opgegeven');
    }
}

Example

83,4,Name,Firstname,1970-01-01,1,3,4,"normal string",1,"dagelijks 2X te verzorgen. brandwonden 3 dagen geleden opgelopen door knalpot van brommer.
voornamelijk 2de graads brandwonden + open wonde",,,,,,"date","date",5,,1,,,1

After "brommer." there is a new line...

faintsignal
  • 1,746
  • 3
  • 19
  • 28
JELLEJ
  • 179
  • 1
  • 12
  • *" it does not show any stuff like , `/n`"* - Are you 100% certain it's `/n`? For newline, the syntax is `\n` if that makes a difference in your code. Showing code and `.csv` sample would help shed some light on the subject. – Funk Forty Niner Jul 21 '14 at 18:08
  • @Fred-ii- added sample and code :) and yes, I meant \n sorry for that – JELLEJ Jul 21 '14 at 18:13
  • 1
    Well, because of your stripslashes(), a simple "\n"—not the control character, just a backslash followed by an n—would be turned into newlines in your output, I think. Can you double-check exactly what's in your source database for the column that's line-breaking? (You may want to check it character by character, e.g. using something like `SELECT ASCII(SUBSTRING(column_name, 34)) ...` to find the exact ASCII value of the 34th character...) It seems a suspicious coincidence that your example newline comes at the exact end of a sentence. I'd guess there's just a newline in your source data. – Matt Gibson Jul 21 '14 at 18:24

2 Answers2

0

OK so I finally found a solution, I've been looking since 2 days ago. It appears that there is some hidden character, because when I make PHP use the function preg_replace and put in the condition "\n-\x0B-\r" the problem is solved.

Thought I'd share it, if someone happens to have this issue in the future.

JELLEJ
  • 179
  • 1
  • 12
0

I used preg_replace to solve this, so here is my solution to the same problem:

// $res: array of strings (with new lines)

foreach($res as $raw){
    $arr[] = preg_replace('/\s+/', ' ', trim($raw)); // Remove newlines from str
}
fputcsv($out, $arr);
qualbeen
  • 1,436
  • 4
  • 14
  • 27