Jump to content

[SOLVED] Code to remove spacing after commas?


mikebyrne

Recommended Posts

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

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

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

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

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

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

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

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

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

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

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

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

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

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.