Skip to main content
Known Participant
May 22, 2014
Question

Remove Carriage Returns for CSV file using PHP & MySQL

  • May 22, 2014
  • 2 replies
  • 3885 views

I have tired everything I can find to remove a carriage return from a MySQL record when creating a CSV file with no success. I have both of these statements and still the record keeps wrapping

<?php $time_candidateNotes = str_replace(array('\r\n', '\r', '\n', '\t', ',', '&hellip', '&quote', '&#13'), ' ', $row_WADAtimes['notes']); ?>

<?php preg_replace('/\\r\\n/', " ", $time_candidateNotes); ?>         

Any help greatly apprenticed.

Thanks

This topic has been closed for replies.

2 replies

BazBat
Participating Frequently
May 23, 2014

Instead of the above do

$rawNotes = $row_WADAtimes['notes'];

$Notesjson = json_encode($rawNotes, true);

$time_candidateNotes = json_decode($Notesjson, true);

Pushing to json and back should fix the formatting. You can always just generate the CSV from JSON going foward but this way will put you back where you started.

Edit: Sorry didn't mean to sound rude 'above' as in your code - I wasn't talking about the answer above.

VernManAuthor
Known Participant
May 29, 2014

This actually returns no value... Not sure what that means

Participating Frequently
May 29, 2014

Syntax error.

$rawNotes = $row_WADAtimes['notes'];

$Notesjson = json_encode($rawNotes);

$time_candidateNotes = json_decode($Notesjson, true);

There shouldn't be a 'true' on the encode, only on decode to turn it back into an object.

If that doesnt work you have have some special chars in your array that will need filtering like so:

html_entity_decode(json_encode($rawNotes));

Best way is to do a var_dump of the $Notesjson to see if it has been populated properly.

Participating Frequently
May 22, 2014

I don't use PHP, but I know there are subtle differences between single and double quotes. Try using str_replace() but wrap the array values with double quotes.