0

I am trying to format my mysql data to csv but for some reason when the code gets to physical address the text inside which is multi-lined brakes the output in the csv thus making it non usable. i've tried trim(), strip_tags() on top of the cleanData() function;

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) . '"';
}

which is echo'd for download with this code;

array_walk($rows,'cleanData');
$trows = implode(",", $rows)."\n";
echo $trows;

Is there a way to concatenate/combine a multi-lined string into a single line string without jumping through hoops?

The data is being retrieved from mysql table - which i loop through and add to the array:

foreach($row as $data)
{
    if($data != '')
    //echo $data.'<br>';
    $tdata = strip_tags(trim($data));
    array_push($rows, $tdata);
    //echo $tdata.'<br>';
}

When it hits the physical address field, i am trying to combine/concatenate the string which is normally entered over a few lines, eg:

The Campus 
Cnr Main & Sloane Street
Bryanston, Johannesburg
02021 South Africa

I want it as "The Campus Cnr Main & Sloane Street Bryanston Johannesburg 02021 South Africa" or "The Campus \n Cnr Main & Sloane Street\n Bryanston, Johannesburg\n 02021 South Africa"

Geraldo Isaaks
  • 156
  • 1
  • 1
  • 20
  • Could you give an example when the bug happens? BTW, one possible typo: `$str = "'$str";` should be `$str = "'$str'";` – biziclop Jul 01 '15 at 12:18
  • Have you looked at http://stackoverflow.com/questions/1241220/generating-csv-file-for-excel-how-to-have-a-newline-inside-a-value ? And you should probably use something like [fputcsv](http://php.net/manual/en/function.fputcsv.php) to generate your csv. Generating compliant csv is a solved problem, use someone else's library. – Chris Wesseling Jul 01 '15 at 14:40
  • @ChrisWesseling nope - fputcsv - doesn't solve the problem... and your link shares nothing with my problem. – Geraldo Isaaks Jul 01 '15 at 15:03

2 Answers2

0

I'm sorry, but your question incomplete, the code snippets are in a muddle and your variable names are inconsistent. For instance, you don't implode(',', $rows) for CSV. You implode(',', $cols) or implode(',', $row), and then implode("\n", $rows) (the latter only if you want the data to be in one single string).

I don't know whether your cleanData() function only fails to replace the newlines (the preg_replace looks ok) or if it's not called at all.

Your code

foreach($row as $data)
{
    if($data != '')
    //echo $data.'<br>';
    $tdata = strip_tags(trim($data));
    array_push($rows, $tdata);
    //echo $tdata.'<br>';
}

is weird since you commented out a statement after an if clause without {}. I don't see any cleanData() calls here. Try to write an SSCCE and I'm quite sure you'll find the problem yourself while doing that.

Oh, to provide another approach for your question. You can do some replaces in SQL already:

select REPLACE(col, "\n", "\\n") from table
steffen
  • 16,138
  • 4
  • 42
  • 81
  • Hi Steffen, thanks for the try but can you try your "solution" and see if it works? It does not work for me, still get the text over a few lines... – Geraldo Isaaks Jul 01 '15 at 16:05
0

I a found solution that fixed my problem from this post;

How to remove line breaks (no characters!) from the string?

Fox's answer fixed it =D

Community
  • 1
  • 1
Geraldo Isaaks
  • 156
  • 1
  • 1
  • 20