Solved

How can I shorten a zip code to its first 3 digits?

Posted on 2012-03-10
3
361 Views
Last Modified: 2012-06-27
Hi guys,
I'm allowing a user to search my  db by using a zip code to find merchants in that zip code.
But I also want to show results of zip codes nearby based on the 5 digit zip code entered.
Shortening the zip code to 4 digits will give me the results I want. I'll also want to expand that search to a much broader area using the same input by shortening that input to 3 digits.

So what do I write after the following:
$5_zip=$_REQUEST['5_zip'];

Thanks for the help,
Sas
0
Comment
Question by:sasnaktiv
3 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 37705795
What kind of database do you have? This is more of a database function than a php function. You have to request all the businesses whose zip codes begin with the first three digits of the requested zip code.

Getting the first three digits is very basic. Use the substr (substring) function. (By the way, php variable names must start with a letter or underscore. See PHP: Basics - Manual, second paragraph.)

$zip3 = substr($_REQUEST['5_zip'],0,3);

Open in new window

0
 
LVL 1

Author Closing Comment

by:sasnaktiv
ID: 37705808
Thank you very much. That's exactly what I needed. Have a good weekend.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37705813
You should know that Zipcodes are not assigned exactly by proximity.  For example, in the area Northeast of Jackson in Northern California, you can find the following adjacent Zipcodes:

95685 - 95665 - 95666 - 95642 - 95255 - 95232
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question