Solved

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

Posted on 2012-03-10
3
366 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

734 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