mikebyrne Posted April 7, 2009 Share Posted April 7, 2009 Im looking to write a piece of code that will remove all the double spacing in my comma separated file and have it as a single space. My file looks like this: ,1517,Phxxxips,Txxxxxsa,,4 Gallxxxxshill,Axxxxy,Co.Kxxxxxare ,1517,Phxxxips,Txxxxxsa,,4 Gallxxxxshill,Axxxxy,Co.Kxxxxxare ,1517,Phxxxips,Txxxxxsa,,4 Gallxxxxshill,Axxxxy,Co.Kxxxxxare I would like my output to look like: ,1517,Phxxxips,Txxxxxsa,,4 Gallxxxxshill,Axxxxy,Co.Kxxxxxare Any help would be great! Quote Link to comment Share on other sites More sharing options...
Fruct0se Posted April 7, 2009 Share Posted April 7, 2009 try preg_replace('/\s{2}/',$input) Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted April 7, 2009 Share Posted April 7, 2009 <?php $lines = file('yourfile.csv'); $output = ""; foreach($lines as $line) { $line = str_replace(" ", " ", $line); $output .= $line; } echo $output; ?> You would just need to save $output back out instead of echo Quote Link to comment Share on other sites More sharing options...
wrathican Posted April 7, 2009 Share Posted April 7, 2009 <?php $lines = file('yourfile.csv'); $output = ""; foreach($lines as $line) { $line = str_replace(" ", " ", $line); $output .= $line; } echo $output; ?> You would just need to save $output back out instead of echo that would only replace a maximum of two spaces. what if it were accidentally more than that? the regex would be better Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 7, 2009 Author Share Posted April 7, 2009 Thanks for the help! How would I send the output to a file rather than on displaying it on screen? Quote Link to comment Share on other sites More sharing options...
Axeia Posted April 7, 2009 Share Posted April 7, 2009 that would only replace a maximum of two spaces. what if it were accidentally more than that? the regex would be better The {2} part in the regular expression made it behave the exact same? If it suites his needs then str_replace is way to go (performance wise). As for writing the output to a file you will want to use file_put_contents() function. <?php file_put_contents( 'filename.txt', $contents ); ?> Make sure that if you're using a unix platform that PHP has write permissions on the directory you're trying to write the file to. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted April 8, 2009 Share Posted April 8, 2009 <?php $lines = file('yourfile.csv'); $output = ""; foreach($lines as $line) { $line = str_replace(" ", " ", $line); $output .= $line; } echo $output; ?> You would just need to save $output back out instead of echo that would only replace a maximum of two spaces. what if it were accidentally more than that? the regex would be better Yeah, I merely answered his question Quote Link to comment Share on other sites More sharing options...
.josh Posted April 8, 2009 Share Posted April 8, 2009 use \s+ instead of \s{2} Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.