mikebyrne Posted April 8, 2009 Share Posted April 8, 2009 I have a comma separated file with all rows ending with the word Co.Kildare but on a large number of theses rows the word appears with a comma at the end ie Co.Kildare, How could I code the replacement of Co.Kildare, with Co.Kildare? <?php $test = file_get_contents("C:\Users\Mike\Desktop\AthyDB.txt"); Replacement code?? file_put_contents('C:\Users\Mike\Desktop\test.txt', $test); ?> Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/ Share on other sites More sharing options...
revraz Posted April 8, 2009 Share Posted April 8, 2009 http://php.net/str_replace Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/#findComment-804721 Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 Something like <?php $test = file_get_contents("C:\Users\Mike\Desktop\AthyDB.txt"); $field=str_replace('Co.Kildare,','Co.Kildare'); file_put_contents('C:\Users\Mike\Desktop\test.txt', $test) ?> Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/#findComment-804727 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 This will get rid of any comma that is at the end of a string: $d = "D,2796,Son,OlerCo.Kildare,"; $currentline=preg_replace('/,$/','', $d); echo $currentline; Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/#findComment-804731 Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 How would I apply $currentline=preg_replace('/,$/','', $d); when taking in a file? Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/#findComment-804735 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Add a '\n' after the comma in the regex and it will pick up the comma then newline. Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/#findComment-804746 Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 Something like this? <?php $test = file_get_contents("C:\Users\Mike\Desktop\AthyDB.txt"); $currentline=preg_replace('/,$/',\n'', $test); file_put_contents('C:\Users\Mike\Desktop\test.txt', $currentline); ?> Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/#findComment-804754 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 $currentline=preg_replace('/(,\n)$/',n'', $test); Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/#findComment-804757 Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 Im getting an error Parse error: parse error in C:\xampp\htdocs\remove.php on line 3 with $currentline=preg_replace('/(,\n)$/''', $test) Quote Link to comment https://forums.phpfreaks.com/topic/153194-solved-replacing-cokildare-with-cokildare/#findComment-804762 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.