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! Link to comment https://forums.phpfreaks.com/topic/152989-code-to-remove-double-spacing/ Share on other sites More sharing options...
Fruct0se Posted April 7, 2009 Share Posted April 7, 2009 try preg_replace('/\s{2}/',$input) Link to comment https://forums.phpfreaks.com/topic/152989-code-to-remove-double-spacing/#findComment-803468 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 Link to comment https://forums.phpfreaks.com/topic/152989-code-to-remove-double-spacing/#findComment-803477 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 Link to comment https://forums.phpfreaks.com/topic/152989-code-to-remove-double-spacing/#findComment-803513 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? Link to comment https://forums.phpfreaks.com/topic/152989-code-to-remove-double-spacing/#findComment-803522 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. Link to comment https://forums.phpfreaks.com/topic/152989-code-to-remove-double-spacing/#findComment-803594 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 Link to comment https://forums.phpfreaks.com/topic/152989-code-to-remove-double-spacing/#findComment-804074 Share on other sites More sharing options...
.josh Posted April 8, 2009 Share Posted April 8, 2009 use \s+ instead of \s{2} Link to comment https://forums.phpfreaks.com/topic/152989-code-to-remove-double-spacing/#findComment-804169 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.