mikebyrne Posted April 4, 2009 Share Posted April 4, 2009 I currently have code thats trimming spaces after commas. Is it possible to modify this code to have a new line after the words "Co.Kildare" as this will be the last word in every line. The code is <?php $txt = file('/path to filename/filename'); // choose correct path location and file foreach ($txt as $i=>$l) $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n"; file_put_contents('/path to filename/filename', implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed... ?> Quote Link to comment Share on other sites More sharing options...
Fruct0se Posted April 4, 2009 Share Posted April 4, 2009 Im not sure if this will work but you can give it a try: <?php $txt = file('/path to filename/filename'); // choose correct path location and file foreach ($txt as $i=>$l) $txt[$i] = preg_replace('/Co.Kildare/', 'Co.Kildare'."\n", $txt[$i]); $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n"; file_put_contents('/path to filename/filename', implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed... ?> Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 That works but only for the last line?? There's 3,000 or so lines and it ignores them all except the last one Quote Link to comment Share on other sites More sharing options...
Fruct0se Posted April 4, 2009 Share Posted April 4, 2009 Hmm, maybe: <?php $txt = file('/path to filename/filename'); // choose correct path location and file foreach ($txt as $i=>$l) $txt[$i] = preg_replace('/Co.Kildare/', 'Co.Kildare'."\r\n", $txt[$i]); $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n"; file_put_contents('/path to filename/filename', implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed... ?> Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 No that just puts an extra line inbetween them. Here's what the last three lines look like L ,2811, Axxxxx, Dxxxxx, 22 Dun Bxxn, Blexxx Roxxxd, Axxxhy, Co.Kildare L ,2812, Axxxxi, Lxxxcia, 22 Dun Brxxn, Blexxxxh Roxxxd, Axxy, Co.Kildare L,2813,Achxxxxxritei,Mxxx,22 Dun Brxxxn,Blxxxxch Rd,Axxxy,Co.Kildare The last line is in the correct format im looking for Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 Any help further help on this would be great! Quote Link to comment Share on other sites More sharing options...
Fruct0se Posted April 4, 2009 Share Posted April 4, 2009 I am guessing at this point but try swapping the two lines: <?php $txt = file('/path to filename/filename'); // choose correct path location and file foreach ($txt as $i=>$l) $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n"; $txt[$i] = preg_replace('/Co.Kildare/', 'Co.Kildare'."\r\n", $txt[$i]); file_put_contents('/path to filename/filename', implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed... ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 4, 2009 Share Posted April 4, 2009 Please don't double post. This topic is a continuation of the topic in http://www.phpfreaks.com/forums/index.php/topic,246192.msg1150820.html#msg1150820 I've locked that one. Ken Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 4, 2009 Share Posted April 4, 2009 I just tried out the code I gave you in the previous thread. I used xampp on WindowsXP. <?php $fn = 'test.txt'; $txt = file($fn); // choose correct path location and file foreach ($txt as $i=>$l) $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n"; file_put_contents($fn, implode('',$txt)); // this will overwrite the filename content with the newer version with spaces removed... ?> on this file: L ,2486, Sam, Smith,, xx Castle Park, Atby, Co.Kil L ,2486, Sam, Smith,, xx Castle Park, Atby, Co.dare L ,2486, Sam, Smith,, xx Castle Park, Atby, Co.Kildare ,2486, Sam, Smith,, xx Castle Park, Atby, Co.Kil L ,2486, Sam, Smith,, xx Castle Park, Atby, Co.dare L ,2486, Sam, Smith,, xx Castle Park, Atby, Co.Kildare The results when viewed in notepad show on one line. When viewed in wordpad they show on separate lines. When you replace <?php $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\n"; ?> with <?php $txt[$i] = implode(',',array_map('trim',explode(',',$l))) . "\r\n"; ?> the file shows correctly with both applications. Ken Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 Thats done the trick! Thanks for all your help 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.