1

How can I remove extra white spaces between words on upload of file

// Allow certain file formats
if($fileType != "csv" ) {
    echo "Sorry, only CSV files are allowed. ";
}
else {
    if (isset($_POST['master'])) {
        (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_master));
    }
    if (isset($_POST['janb1'])) {
        (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_janb1));
    }
}

This is part of my csv results note the extra spaces between Stockwell Constr - should only have one space

07-01-2020,Pitstop Couriers,-2377.05,7313.5
07-01-2020,Stockwell       Constr,-1993.56,5319.94
07-01-2020,Sphe Salary,-500,4819.94
07-01-2020,Frans Salary,-200,4619.94
07-01-2020,Job Salary,-200,4419.94

1 Answers1

0

The workflow using fgetcsv should be the correct way. You would get back a pointer which allows you to call foreach on the rows. From there all you would want to do is run a preg_replace($cell_content, /s+/, “ “) to replace any white space with a single space. Not sure what is happening in your function to have caused the addition of tab-sized white space, but it will possibly be reading out some form of “\t” character from the document itself.

Aaron Morefield
  • 952
  • 10
  • 18