Forum Moderators: coopster

Message Too Old, No Replies

Clean up user submitted addresses (regex)

php string replace regex user submitted data

         

theMaab

5:06 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



Hi there,

I have a problem where I need to clean up user submitted addresses.

The issue is not everyone is putting a 'space' behind their commas.

example:
12345 ABC Street,Midland,TX,79708,USA

needs to be:
12345 ABC Street, Midland, TX, 79708, USA

I need to be able to insert a space after each comma.
Sometimes the spaces might be there, sometimes not.

I know I'll need to use the Regex String Replace function, I just do not know how to do this with regex.

Could anyone help out?
Thanks,
James

eelixduppy

6:08 pm on Aug 26, 2008 (gmt 0)



preg_replace('/,\s?/', ', ', $string);
should work.

theMaab

7:01 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



Thanks eelixduppy,
Worked great!

eelixduppy

7:05 pm on Aug 26, 2008 (gmt 0)



If you want to strip excess spacing at the same time so that everything is the same, then you can change the pattern to the following and it should work just the same as before, but strip the extra stuff, as well:

$pattern = "/,\s*/";

Anyway, glad it worked out. :)