Upload CSV file how to remove extra spaces

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

Recursively replace 2 spaces with 1 space.

how do I do this in my upload file

Well, one way would be

while (strstr($string, '  ')) {
	$string = str_replace('  ', ' ', $string);
}

I’m not sure if there’s a more elegant solution.

Ok let me try

So, there is no way on upload to remove, have to read csv file in an array then remove and rewrite file

Try this:

$tmp =  $target_file_master;

$newLine = "";
  foreach($tmp as $key => $line ):
    $line[$key] = str_replace($line, str_replace("  ", " ", $line));
  endforeach;

$target_file_master = $line;

(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_master));    

Please note I have not tried this because I am late for a lunch appointment :slight_smile:

thanx will try

not working, see you have declare $newLine = “”; but did not use it

You would start by writing a function that consolidates multiple spaces into a single space. Then, at the two places where you are moving the uploaded file now, you would instead -

  1. Test if the source file is a valid uploaded file, for security purposes. Move_uploaded_file() did this test for you, but you are no longer using move_uploaded_file().
  2. Read the contents of the source file into a string.
  3. Call the function that consolidates the spaces.
  4. Write the resulting string to the destination file.
// consolidate multiple spaces to a single space
function consolidate_spaces($string)
{
		$previous_string = '';
	
	// loop until the strings are the same, no changes
	while($string != $previous_string)
	{
		$previous_string = $string; // remember the starting value
		$string = str_replace('  ', ' ', $string); // remove multiple spaces
	}
	return $string;
}

Example code for where the ‘master’ file move_uploaded_file() statement is at now -

if (is_uploaded_file($_FILES["fileToUpload"]["tmp_name"]))
{
	$string = file_get_contents($_FILES["fileToUpload"]["tmp_name"]);
	$string = consolidate_spaces($string);
	file_put_contents($target_file_master,$string);
} else {
	echo "The tmp file was not a valid uploaded file."; // this means that something managed to place a file onto the server and set the $FILES ... ['tmp_name'] variable to it before your code was executed.
}

It is not uploading file I have replace

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));
    }

With

if (is_uploaded_file($_FILES["fileToUpload"]["tmp_name"]))
{
	$string = file_get_contents($_FILES["fileToUpload"]["tmp_name"]);
	$string = consolidate_spaces($string);
	file_put_contents($target_file_master,$string);
} else {
	echo "The tmp file was not a valid uploaded file."; // this means that something managed to place a file onto the server and set the $FILES ... ['tmp_name'] variable to it before your code was executed.
}

I have this part right at the bottom of file cant I implement it here to remove extra spaces to both master and temp file

$row = 1;
     $file = fopen("temp.csv","w");

    if (($handle = fopen("master.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            $row++;
            echo $data[1] . "<br />\n";
            $line = $data[1];
            $line .=  "\n";
            fputs($file, $line);
       }
      fclose($handle);
   }
   fclose($file);

$lines = file("temp.csv");
$lines = array_unique($lines);
sort($lines);
file_put_contents('temp.csv', implode($lines));

Have you considered fixing the source of the csv file? The upload process is not adding the spaces. Whatever is generating the file is doing so. Fix that and the problem goes away.

2 Likes

It is really not a problem the extra white spaces it is browser explorer that omit the extra white space and when comparing browser list box with master.csv file it gives error as the browser list box extra white spaces is omitted but in file it is still there. So what I need is to read the master.csv file in an array then remove extra white spaces and then write file

Why do you want to read it as an array? Why not just read the entire file?

How do I read entire file, remove extra white spaces and write back to csv file

You’ve already been given the answer to this. (literally, the three parts of your sentence are the three lines of code mab gave you here).

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.