Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Remove Carriage Returns for CSV file using PHP & MySQL

Explorer ,
May 22, 2014 May 22, 2014

Copy link to clipboard

Copied

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

TOPICS
Server side applications

Views

3.5K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 22, 2014 May 22, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 23, 2014 May 23, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 29, 2014 May 29, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 29, 2014 May 29, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 29, 2014 May 29, 2014

Copy link to clipboard

Copied

Just curious, did you try switching from single to double quotes?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 29, 2014 May 29, 2014

Copy link to clipboard

Copied

I did try switching, did nothing. This is the final result, as I needed to get rid of carriage returns, commas, etc

I don't understand why I needed all this. Can someone explain what json_encode/decode does? Or why I would need all this code?

$rawCandidateName = $row_WADAtimes['candidateName'];

$CandidateNamejson = json_encode($rawCandidateName);

$time_candidateName = json_decode($CandidateNamejson, true);

$time_candidateName = trim(preg_replace("/,\n\r|\r\n/", "", $time_candidateName));

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

Thanks

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 30, 2014 May 30, 2014

Copy link to clipboard

Copied

$array = array('a','b','c');

var_dump($array);

echo '<br>';

$json = json_encode($array);

var_dump($json);

echo '<br>;

$backtoanarray = json_decode($json,TRUE);

var_dump($backtoanarray);

It literally just turns an array into a JSON object and then back again into an array, its a quick way to fix formatting issues rather than trying to filter it using patterns or string replace.

http://www.php.net/manual/en/language.types.array.php

PHP: json_encode - Manual

Use var_dump to debug and add this at the top of your php to report errors

error_reporting(E_ALL);

ini_set("display_errors", 1);

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 24, 2015 Apr 24, 2015

Copy link to clipboard

Copied

Hi! Got a trial and can't find the "Databases" in WIndow>

have Mysql already running on my computer though.

Does anybody know if it is because of a trial?

Thanks!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 24, 2015 Apr 24, 2015

Copy link to clipboard

Copied

LATEST

There are no longer database (server) behaviors included in Dreamweaver. You must write your own code or purchase a third party extension.

By the way, adding your question to an old post that is unrelated to your question is not advised.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines