SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HELP ME ON HOW TO REDUCE or REMOVE SPACES IN csv

    How can I remove blank spaces in CSV files that I am generating using php code. Iam have come up with a php script that is generating a batch of records, but when I open the records in micrsoft excel or notepad Iam getting more blank space than necessary.
    Below is the php code Iam using to extract from mysql. The part in bold is the one to do with spaces. So may some one help.

    <?php

    while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {
    if ((!isset($value)) OR ($value == "")) {
    $value = "\t";
    } else {
    $value = str_replace('"', '""', $value);
    $value = '' .$value. ',' . "\t";
    }
    $line .= $value;
    }
    $data .= trim($line)."\n";
    }
    $data = str_replace("\r","",$data);
    if ($data == "") {
    $data = "\n(0) Records Found!\n";
    }
    header("Cache-control: private");
    header("Content-type: application/force-download");
    header("Content-Disposition: attachment; filename=TRVP$batchnumber.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\n$data";
    ?>

  2. #2
    SitePoint Zealot krt's Avatar
    Join Date
    Sep 2005
    Location
    Australia
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you give us an example of the generated CSV. mysql_fetch_row() trims spaces for you so the only space should be the tab after each comma.

  3. #3
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When a file is genearated the output looks like this below. BUT I WANT AN OUT PUT LIKE or SIMILAR TO THE ONE IN BOLD BELOW

    2020 " 2006-06-20" " 1" " P001" " 27400000" " 223"
    987654 " 2006-06-21" " 2" " P146" " 200000444" " 223"
    987654 " 2006-06-21" " 10" " P146" " 81552" " 223"
    2020 " 2006-06-21" " 3" " P146" " 102400000" " 223"
    70308 " 2006-06-21" " 6" " P146" " 867500" " 223"
    70306 " 2006-06-21" " 7" " P117" " 102100000" " 223"
    987654 " 2006-06-21" " 20" " P182" " 1000003552" " 223"
    70308 " 2006-06-21" " 9" " P001" " 2147483647" " 223"


    2020,2006-06-20,1,3001,7400000,900,23
    987654,2006-06-21,2,P146, 200000444,223
    987654,2006-06-21,10,P146,81552,223"

  4. #4
    SitePoint Zealot krt's Avatar
    Join Date
    Sep 2005
    Location
    Australia
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The data source gives that, you can get rid of the quotes and spaces if you want
    Change (the bolded line):
    $value = '' .$value. ',' . "\t";
    To:
    $value = trim($value,' "') . ',';


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •