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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/ 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 } Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803736 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! Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803739 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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803844 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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803853 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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803859 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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803878 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?? Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803971 Share on other sites More sharing options...
blueman378 Posted April 7, 2009 Share Posted April 7, 2009 Read this Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803973 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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803986 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); Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-803993 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. Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804003 Share on other sites More sharing options...
blueman378 Posted April 7, 2009 Share Posted April 7, 2009 thats better jsut had to change something. Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804008 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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804010 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)); Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804362 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); Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804364 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!) Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804376 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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804388 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?? Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804396 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 Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804398 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'); Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804403 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. Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804410 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!! Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804425 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! Link to comment https://forums.phpfreaks.com/topic/153031-solved-replacing-single-letters-with-sentence/#findComment-804428 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.