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
https://forums.phpfreaks.com/topic/244190-parsing-a-zipcode-field-from-a-database/
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;
?>

Archived

This topic is now archived and is 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.