Jump to content

parsing a zipcode field from a database


chadrt

Recommended Posts

I have some pretty complex scripts that pull data from an FCC database (they provide to the public) and they have in the past year been migrating the data to include a zip+4 but the zips are all stuck together for example:

 

80112-9999 would be displayed like this 801129999

 

What I would like to do is have it take the variable that contains the zip code and IF the zip is 9 digits place a dash "-" between 5th and 6th digits but IF the zip only contains 5 digits then it would only show the 5 digit zip code.  Can this be done?

 

Chad

 

Link to comment
Share on other sites

You can REGEX (preg_replace), however I think you can also use a string function

 

if not, the least you can do

 

<?php
# Your Zip Code
$zip=801129999;
# Check if the length == 9
if(strlen($zip) == 9){ $zip = substr($zip,0,5) . '-' . substr($zip,5,9); }
print $zip;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.