mikebyrne Posted April 7, 2009 Share Posted April 7, 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) Is it possible to code this instead of manually replacing them?? Ideally I would like the output to pass into a new file Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 preg_replace('^D','Dail European Parliament and Local Elections only'); preg_replace('^S','Post or special arrangement only'); preg_replace('^L','Local Elections only'); preg_replace('^E','European Parliament and Local Elections only'); That might work (my first attempt at regex) but if you're parsing the CSV file then just replace the first element. switch ($element[0]) { case 'D':$element[0]='Dail European Parliament and Local Elections only';break; case 'S':$element[0]='Post or special arrangement only';break; //...and so-on } Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 7, 2009 Author Share Posted April 7, 2009 Im a little confused as to which one to try? Also how do I code so it takes in the file and outputs to another? ps thanks for the help so far! Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 7, 2009 Author Share Posted April 7, 2009 Anymore help on this matter would be great Quote Link to comment Share on other sites More sharing options...
blueman378 Posted April 7, 2009 Share Posted April 7, 2009 The thing is you are asking us to code the whole thing for you without showing any attempt at doing it yourself. You will not be given more help unless: a) you post in the freelance section b) you atleast attempt to wrote it yourself then come back when you have a problem. Matt Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 7, 2009 Author Share Posted April 7, 2009 No, as stated in my last post Im just asking for advice on which option to go with the preg replace or by using the switch Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 7, 2009 Author Share Posted April 7, 2009 I've tried with the following code <?php $fields=file('C:\Users\Mike\Desktop\AthyDB.txt'); foreach($fields as $i=>$field) { $field=preg_replace('^D','Dail European Parliament and Local Elections only'); $field=preg_replace('^S','Post or special arrangement only'); $field=preg_replace('^L','Local Elections only'); $field=preg_replace('^E','European Parliament and Local Elections only'); $fields[$i]=$field; } file_put_contents('C:\Users\Mike\Desktop\test.txt',implode("\r",$fields)); ?> but im getting the following error Warning: Wrong parameter count for preg_replace() in C:\xampp\htdocs\replace.php on line 5 Warning: Wrong parameter count for preg_replace() in C:\xampp\htdocs\replace.php on line 6 Warning: Wrong parameter count for preg_replace() in C:\xampp\htdocs\replace.php on line 7 Warning: Wrong parameter count for preg_replace() in C:\xampp\htdocs\replace.php on line 8 Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 7, 2009 Author Share Posted April 7, 2009 Cant see where I'm going wrong with this?? Quote Link to comment Share on other sites More sharing options...
blueman378 Posted April 7, 2009 Share Posted April 7, 2009 Read this Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 7, 2009 Author Share Posted April 7, 2009 Thanks, very useful I've tried that and it seemed to lean towards an older version of php being the issue but im all up to date Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted April 7, 2009 Share Posted April 7, 2009 http://uk2.php.net/str_replace str_replace() might work better if you just run it on the one character field. No regex needed. $var = str_replace("{char}","{full text}",$var); Quote Link to comment Share on other sites More sharing options...
blueman378 Posted April 7, 2009 Share Posted April 7, 2009 basically you have to pass it the string you want it to run on. without showing any attempt at doing it yourself (didnt mean to sound harsh jut explaining the reason why you were not getiing any more help, no code = no help as a general rule) but anyway try this differnt to yours cause its not using an array but hey if it works <?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'); fclose($handle); } ?> Please make a backup of your file first as this is untested and may create unintention bugs. Quote Link to comment Share on other sites More sharing options...
blueman378 Posted April 7, 2009 Share Posted April 7, 2009 thats better jsut had to change something. Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 7, 2009 Author Share Posted April 7, 2009 No problem Blueman378 it was just frustrating me I'll try the code out in a bit, I'm just away from the other pc Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 with ths code <?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'); fclose($handle); } ?> Im getting a parse error "Parse error: parse error in C:\xampp\htdocs\letters.php on line 10" Line 10 is $currentline=preg_replace('^S','Post or special arrangement only', $currentline)); Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 You've got two brackets at the end of three of those lines when you should only have one - should be like this: $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); Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 Sorry! Good spot Im now getting the error Warning: preg_replace() [function.preg-replace]: No ending delimiter '^' found in C:\xampp\htdocs\letters.php on line 9 Warning: preg_replace() [function.preg-replace]: No ending delimiter '^' found in C:\xampp\htdocs\letters.php on line 10 Warning: preg_replace() [function.preg-replace]: No ending delimiter '^' found in C:\xampp\htdocs\letters.php on line 11 Warning: preg_replace() [function.preg-replace]: No ending delimiter '^' found in C:\xampp\htdocs\letters.php on line 12 Should the code be '^S,' or something?? (No I just tried it!) Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Try changing them to include /'s /^S/ etc. regex isn't really my thing - learning as much here myself Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 Sorry, what do you mean by changing them to include /'s?? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Like this... $currentline=preg_replace('/^S/','Post or special arrangement only', $currentline); You'll have to do that for each line Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 That seems to have fixed that issue but im now getting the error Warning: file_put_contents() expects at least 2 parameters, 1 given in C:\xampp\htdocs\letters.php on line 16 Line 16 is file_put_contents('C:\Users\Mike\Desktop\test.txt'); Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 http://uk.php.net/file_put_contents Your line of code would require the data to written out as the second parameter after the filename. Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 8, 2009 Author Share Posted April 8, 2009 I've changed it to <?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); } ?> And it is now working! Thanks for all the help!! Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Glad to see you got it sorted in the end! 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.