mikebyrne Posted April 8, 2009 Share Posted April 8, 2009 I have a file that looks something like the following: D,2796,Son,Oler,13 Dun Bnn,Bch Road,Ahy,Co.Kire S,2797,Gerty,Laurce,15 Dun Brn,Bleach ad,Ahy,Co.Kilde L,2801,Mazse,Saras,17 Dn Brn,Blch Rod,Aty,Co.Kilre E,2808,Esjo,Leel,21 Dun Bnn,Blach Road,Ay,Co.Kilre What I am trying to do is replace the single character letters "D","S","L" & "E" with the following sentences Dail European Parliament and Local Elections only (instead of D) European Parliament and Local Elections only (instead of E) Local Elections only (instead of L) Post or special arrangement only (instead of S) with lots of help we compiled the following <?php $rewrote = ""; $handle = fopen("C:\Users\Mike\Desktop\AthyDB.txt", "r+"); // Open file to read it read. if ($handle) { while (!feof($handle)) // Loop til end of file. { $currentline = fgets($handle, 4096); // Read a line. $currentline=preg_replace('/^D/','Dail European Parliament and Local Elections only', $currentline); $currentline=preg_replace('/^S/','Post or special arrangement only', $currentline); $currentline=preg_replace('/^L/','Local Elections only', $currentline); $currentline=preg_replace('/^E/','European Parliament and Local Elections only', $currentline); $rewrote .= $currentline; $rewrote .= "\n"; } file_put_contents('C:\Users\Mike\Desktop\test.txt', $rewrote); fclose($handle); } ?> It seems that the replacing of "D" produces "Dail European Parliament and Local Elections onlyail European Parliament and Local Elections only" Any idea what's gone wrong? It also seems to happen for "L" a few times but not everytime?? (Strange)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/153175-solved-strange-values-repeating/ Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 EDIT: Nvm doesn't work. Your code works for me with your sample data. Quote Link to comment https://forums.phpfreaks.com/topic/153175-solved-strange-values-repeating/#findComment-804655 Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 <?php $rewrote = ""; $handle = fopen("C:\Users\Mike\Desktop\AthyDB.txt", "r+"); // Open file to read it read. if ($handle) { while (!feof($handle)) // Loop til end of file. { $currentline = fgets($handle, 4096); // Read a line. $currentline=preg_replace('/^D$/','Dail European Parliament and Local Elections only', $currentline); $currentline=preg_replace('/^S$/','Post or special arrangement only', $currentline); $currentline=preg_replace('/^L$/','Local Elections only', $currentline); $currentline=preg_replace('/^E$/','European Parliament and Local Elections only', $currentline); $rewrote .= $currentline; $rewrote .= "\n"; } file_put_contents('C:\Users\Mike\Desktop\test.txt', $rewrote); fclose($handle); } ?> That seems to fix the repeating problem but the code seems to stop replacing the letters after 2,000+ Very strange. Any idea why it stops replacing? Quote Link to comment https://forums.phpfreaks.com/topic/153175-solved-strange-values-repeating/#findComment-804661 Share on other sites More sharing options...
sasa Posted April 8, 2009 Share Posted April 8, 2009 <?php $test = file_get_contents("C:\Users\Mike\Desktop\AthyDB.txt"); $repl = array( 'D' => 'Dail European Parliament and Local Elections only', 'E' => 'European Parliament and Local Elections only', 'L' => 'Local Elections only', 'S' => 'Post or special arrangement only' ); $test = explode("\n", $test); foreach ($test as $k => $v){ $test[$k] = $repl[substr($v,0,1)].substr($v,1); } $test = implode("\n", $test); file_put_contents('C:\Users\Mike\Desktop\test.txt', $test); ?> Quote Link to comment https://forums.phpfreaks.com/topic/153175-solved-strange-values-repeating/#findComment-804664 Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 That seems to have fixed it Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/153175-solved-strange-values-repeating/#findComment-804666 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.