Using the if (strlen...) approach you came up with, along with the original regex I posted on page 1, now, I am able to get ID formats as well as zip code working correctly.
Can you come up with another clever way to handle address?
As you know, address could come in the form of 780 Bill Way or something like that and given that we have a rule that a space needs to be inserted after the first 2 digits, then 780 Bill Way could look more like 78 0 Bi LL way.
Is there a way to indicate that if the digit is not up to 6 in length, no formatting required?
This way, not only does it handle zip code, it will handle addresses as well?
Oh, sure. Instead of just looking for strlen($id) < 6, instead use a regular expression to allow you to count ONLY digits.
Don't trust me on this; experiment. But maybe this:
if ( strlen( preg_replace( '[^\d]', "", $id ) ) > 5 )
... do the other replaces ...
See the logic? That if statement says: "Take the $id and replace EVERY non-digit character with the blank string; then get the length of what remains--which will be all digits--and only continue if it is longer than 5 characters."
*** BUT ***
What about an address such as
1313 East 12th Ave
Strip that to all digits and you have 131312 which is 6 digits. *KABLOOEY*
Maybe a better way would be to count the *NON* digit characters and reject if there are 4 or more. (3 is okay...could be 33N888111GG881, meaning non-digit is NGG) *BETTER*, just count the characters that are *NOT* N or D or G or space!!
So it still needs more work.
if ( strlen( preg_replace( '[^\d]', "", $id ) ) > 5
&& strlen( preg_replace( '[NnDdGg\d\s]', "", $id ) == 0 )
... process more ...
That says "If there are 6 or more digits *AND* there are *NO* characters other than N, D, G, space, and digits *THEN* do more processing."
No guarantees. Experiment.
Last edited by Bill Wilkinson; 02-01-2013 at 12:24 AM.