Dave brought up something I forgot, what if the "-" is already in the sip
code;

Modify the code as follows;

If(strlen($zipcode) <= 5) $zipcode = sprintf("%05d",$zipcode); 
Else {
     $zipcode = sprintf("%09d",str_replace("-","",$zipcode));
     $zipcode = substr($zipcode,0,5)."-".substr(zipcode,5);
}

> -----Original Message-----
> From: Warren Vail [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, August 21, 2008 8:08 PM
> To: 'Keith Spiller'; php-general@lists.php.net
> Subject: RE: [PHP] Restore Leading Zeros in Zip Codes
> 
> I'd try something like;
> 
> If(strlen($zipcode) <= 5) $zipcode = sprintf("%05d",$zipcode); Else {
>     $zipcode = sprintf("%09d",$zipcode);
>     $zipcode = substr($zipcode,0,5)."-".substr(zipcode,5);
> }
> 
> This isn't real elegant, but it should do the trick.  You may 
> notice that the 10 character zip code is not 10 digits but 
> rather 5 plus 4, or 9 digits.
> 
> Warren Vail 
> 
> > -----Original Message-----
> > From: Keith Spiller [mailto:[EMAIL PROTECTED]
> > Sent: Thursday, August 21, 2008 7:44 PM
> > To: php-general@lists.php.net
> > Subject: [PHP] Restore Leading Zeros in Zip Codes
> > 
> > Hi,
> > 
> > RE:  Restore Leading Zeros in Zip Codes
> > 
> > Does anyone happen to have a script that will restore the leading 
> > zeros in a mixed data set of 5 digit zip codes and 10 digit zip+4 
> > codes?  Any suggestions?
> > 
> > Thanks,
> > 
> > 
> > Keith
> > 
> 
> 
> --
> PHP General Mailing List (http://www.php.net/) To 
> unsubscribe, visit: http://www.php.net/unsub.php
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to