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... ?> Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/ 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... ?> Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801119 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 Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801157 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... ?> Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801162 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 Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801164 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! Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801174 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... ?> Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801180 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 Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801181 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 Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801195 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 Link to comment https://forums.phpfreaks.com/topic/152529-solved-new-line-after-keyword/#findComment-801201 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.