mikebyrne Posted April 3, 2009 Share Posted April 3, 2009 I currently have a text file in the below format: L ,2486, Sam, Smith,, xx Castle Park, Atby, Co.Kildare Is there any code out the that will help me remove the spaces after the commas so the line will look like: L,2486,Sam,Smith,,xx Castle Park,Atby,Co.Kildare Link to comment Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 $txt=str_replace(', ',',',$txt); Pass in $txt and it returns $txt with spaces removed. Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 The only issue with that solution is that the OP wants the space after the intitial L removed as well, despite asking to remove the spaces after the commas (so admittedly, the OP is contradictary): L,2486,Sam,Smith,,xx Castle Park,Atby,Co.Kildare try: $txt = preg_replace('#\s*,\s*#', ',', $txt); EDIT: Of course, the above pattern means it will even try to replace a comma that is not surrounded by spaces with a comma.. but it will still do the job with imperceptable hinderance... one could resort to alternations... but those tend to be generally slower. Link to comment Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 One day I will learn regex but for now - they cause my head to hurt :'( Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 One day I will learn regex but for now - they cause my head to hurt :'( regex looks intimidating, but in reality, it isn't that hard.. You can learn regex by visiting sites such as regular-expressions.info. You can also read a memeber written tutorial here as well as read the resources page. EDIT - I went to your site.. ah.. Amiga... I miss those days.. me and my Amiga 2000 were inseperable! Damn, I miss those days a lot! Link to comment Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 I've already been there. Starts off OK but then I get lost not long after starting. I've lost count the number of tutorials I've tried following and nothing has worked. I've even got RegexBuddy installed on this machine to help me develop regex patterns and I can never get one matching what I want! Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 Well, all I can suggest is keeping at it.. start small, and only move forward with more complex problems once you feel comfortable. One step at a time. Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2009 Author Share Posted April 3, 2009 At present I've 3,000 lines in my file, will this matter in terms of the code? Will I need to pass the contents into a db first and run the php? Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 At present I've 3,000 lines in my file, will this matter in terms of the code? Nah, not really. Will I need to pass the contents into a db first and run the php? I would do all replacing first, then store it into your db. Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 3, 2009 Share Posted April 3, 2009 Here's a way to do it without regular expressions: <?php $txt = 'L ,2486, Sam, Smith,, xx Castle Park, Atby, Co.Kildare'; $txt = implode(',',array_map('trim',explode(',',$txt))); echo $txt; ?> Ken Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 Yup, that would do it too! Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2009 Author Share Posted April 3, 2009 How do I apply the code to the text file?? Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 Make a backup of your file first, just in case... then you can use the following as an example: $txt = file_get_contents('/path to filename/filename'); // choose correct path location and file $txt = implode(',',array_map('trim',explode(',',$txt))); file_put_contents('/path to filename/filename', $txt); // this will overwrite the filename content with the newer version with spaces removed... Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2009 Author Share Posted April 3, 2009 Stupid question Number 2: How will I execute the code? Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 Hmm.. I don't follow.. aren't you doing this from a .php page? Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2009 Author Share Posted April 3, 2009 I am indeed, So therefore I just load the page from the browser? Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 Yes, that is how .php pages are run. If you are too new to PHP, I would suggest taking a step back and futhering your self in the basics of php through googling online tutorials as an example (I'm not trying to be rude nor difficult). But this is very basic. Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2009 Author Share Posted April 3, 2009 I was just used to coding buttons etc. The code works but it gets rid of the single line format and groups everything together. Is there anything I can do to fix this? Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 Is it all one line when you open the file? Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 3, 2009 Share Posted April 3, 2009 If you have newlines in your initial text file, the last code snippet will preserve those.. so on a sample .txt file I made, which looked like this: 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 The last snippet will successfully convert the file to: 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 If you are eching this out on screen, yes, it will all be on a line.. but within the file, newlines should be preserved. Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2009 Author Share Posted April 3, 2009 Im using the code $txt = file_get_contents('/path to filename/filename'); // choose correct path location and file $txt = implode(',',array_map('trim',explode(',',$txt))); file_put_contents('/path to filename/filename', $txt); // this will overwrite the filename content with the newer version with spaces removed... And the output file is not on separate lines, just one after the other ie ,1,Caxxn,Cxxxy,Wayside,Ardxxxgh,Axxxy,Co.Kildare,2,Caxxxn,Lxxxxm,Wayside,Ardxxxxigh,Axxy,Co.Kildare Co.Kildare will always be at the end of the line if thats any help with separating lines Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 3, 2009 Share Posted April 3, 2009 You can try changing the code to be: <?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... ?> Ken Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2009 Author Share Posted April 3, 2009 That produces the same result I think the problem is that the first field can be blank i.e the first line ,2823, Fxxxxx, Caroxxx, 1 Dun Bxxxx, Forxxxxxton Drive, Axxxy, Co.Kildare L ,2824, Oxxxxx, Axxtutu, 2 Dun Brxxx, Forxxxxxxxxton Drive, Axxxy, Co.Kildare Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 3, 2009 Share Posted April 3, 2009 Two questions: 1) What hardware is the PHP running on? 2) How are you viewing the resulting file? If you're using Windows to view the file and you're using Notepad, try using Wordpad instead. Ken Link to comment Share on other sites More sharing options...
mikebyrne Posted April 4, 2009 Author Share Posted April 4, 2009 Im running the php via an apachi server (xampp). I run the file using Firefox and I create the php page using dreamweaver Link to comment Share on other sites More sharing options...
Recommended Posts