Solved

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

Posted on 2012-03-10
3
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
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 dynamically set the form action using jQuery.

624 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