chadrt Posted August 8, 2011 Share Posted August 8, 2011 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 More sharing options...
phpSensei Posted August 8, 2011 Share Posted August 8, 2011 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 https://forums.phpfreaks.com/topic/244190-parsing-a-zipcode-field-from-a-database/#findComment-1254099 Share on other sites More sharing options...
thereaper87 Posted August 8, 2011 Share Posted August 8, 2011 I posted almost the exact same thing has phpSensei! lol Link to comment https://forums.phpfreaks.com/topic/244190-parsing-a-zipcode-field-from-a-database/#findComment-1254101 Share on other sites More sharing options...
chadrt Posted August 8, 2011 Author Share Posted August 8, 2011 That is exactly what I needed! Thank you so very much... Chad Link to comment https://forums.phpfreaks.com/topic/244190-parsing-a-zipcode-field-from-a-database/#findComment-1254174 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.