Question

Physical address Validator PHP

Asked by: movieprodw

Hello,

I have a field where someone fills out their physical address and then it goes into the db.

The problem I am having is that google is not allowing some of these addresses to be accepted into the google base as it claims they are invalid.

Here is an example of some that are 'invalid'
10910 Braemor Dr., Haslet, TX, 76052, USA
1310E 700S, Wolcottville, IN, 46795, USA
Od Chula Road, Amelia Court House, VA, 23002, USA
401 Pinehurst Ave., Portland, TX, 78374, USA
1005 Lake Shadow Dr., Dallas, NC, 28034, USA

Do you know what makes these invalid and if so do you have a solution so that I can 'validate' them before they get submitted?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-31 at 16:33:52ID24861274
Tags

php

Topic

PHP and Databases

Participating Experts
1
Points
500
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Car physics - Tire/road friction, skidding, and turning
    I'm developing my first game containing player controlled cars, and therefore ofcourse I need proper car physics. I've got basic car accelleration and turning done, however, no skidding is currently done. Basically, when a driver drives a car at n speed and applies the brake...
  2. Running with no AV??
    Ok - here are my security settings that I had done recently to secure Win2000 so I dont have to run a AV.... 1. RestrictAnonymous is set to = (2) 2. Disabled DCOM service 3. Disabled Telnet, Net Meeting, Remote Desktop and a few others... 4. Disabled File Sharing and NetBios...
  3. Are keyloggers permissable in court?
    We have the pc of a user we suspect was up to malicious things on a customer site. We have the PC, but it has been reformatted (low level) by the perpetrator. We had keylogging software on the machine, that would periodically send emails back to headquarters. The PC has be...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Ray_PaseurPosted on 2009-10-31 at 17:05:03ID: 25712120

You might try running these through a geocoder to get a normalized address.  I'll post a code sample in a moment.

 

by: movieprodwPosted on 2009-10-31 at 17:09:13ID: 25712130

Okay, that would be great!

 

by: Ray_PaseurPosted on 2009-10-31 at 17:13:25ID: 25712140

Just clicking through, this is what I get - three could be "corrected" by Google Maps.

<?php // RAY_temp_mpw.php
error_reporting(E_ALL); 
// TEST DATA
$arr[] = '10910 Braemor Dr., Haslet, TX, 76052, USA';
$arr[] = '1310E 700S, Wolcottville, IN, 46795, USA';
$arr[] = 'Od Chula Road, Amelia Court House, VA, 23002, USA';
$arr[] = '401 Pinehurst Ave., Portland, TX, 78374, USA';
$arr[] = '1005 Lake Shadow Dr., Dallas, NC, 28034, USA';  
// REVISED DATA
$new[] = '10910 Braemoor Dr., Haslet, TX, 76052, USA';
$new[] = '1310 E 700 S, Wolcottville, LaGrange, Indiana 46795';
$new[] = 'Old Chula Road, Amelia Court House, VA, 23002, USA';
$new[] = 'PROBABLY NG: 401 Pinehurst Ave., Portland, TX, 78374, USA';
$new[] = 'PROBABLY NG: 1005 Lake Shadow Dr., Dallas, NC, 28034, USA';

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-10-31 at 17:16:35ID: 25712152

Install this and try feeding the addresses to it.  You may be able to adapt something like this to your needs.  I've found considerable variability between the geocoders, but one or the other is almost always fairly close.

<?php // RAY_class_SimpleGeoCoder.php
error_reporting(E_ALL); 
// API KEYS, ETC., IF REQUIRED
// require_once('_config.php'); 

// PUT UP A FORM TO RECEIVE THE ADDRESS INFORMATION
if (empty($_GET))
{    ?>
<html><head><title>Yahoo/Google SimpleGeoCoder Demo</title></head>
<body>
<form method="get">
Addr: <input type="text" name="a" autocomplete="off" />
City: <input type="text" name="c" autocomplete="off" />
ST:   <input type="text" name="s" autocomplete="off" size="2" />
Zip:  <input type="text" name="z" autocomplete="off" size="8" />
<input type="submit" name="_go" value="go" />
</form>
</body>
</html>
<?php
    die();
} 

// A FREEFORM LOCATION STATEMENT IS OPTIONAL - GET ADDRESS, CITY, STATE, ZIP FROM THE URL
$location = '';
if ($_GET["a"] != '') { $location .= $_GET["a"] . ' '; }
if ($_GET["c"] != '') { $location .= $_GET["c"] . ' '; }
if ($_GET["s"] != '') { $location .= $_GET["s"] . ' '; }
if ($_GET["z"] != '') { $location .= $_GET["a"] . ' '; }
$location = trim($location); 

// PREPARE THE GEOCODER
$demo          = new SimpleGeoCoder;
$demo->address = $_GET["a"];
$demo->city    = $_GET["c"];
$demo->state   = $_GET["s"];
$demo->zip     = $_GET["z"]; 
// TEST THE YAHOO! GEOCODER
$demo->geocodeYahoo();
echo "<pre>\n\nYAHOO! ";
print_rr($demo);
echo "</pre>\n"; 
// TEST THE GOOGLE GEOCODER
$demo->geocodeGoogle();
echo "<pre>\n\nGOOGLE ";
print_rr($demo);
echo "</pre>\n"; 

// ALL DONE
die(); 
 
// SIMPLE GEOCODER CLASS
class SimpleGeoCoder
{ 
// DECLARE THE INPUT DATA
    public $location; // USE THIS FOR A FREEFORM QUERY, OR USE THE PARTS
    public $address;
    public $city;
    public $state;
    public $zip; 
// DECLARE THE WORKING DATA
    private $precis; // TEXTUAL PRECISION FROM YAHOO
    private $yfp, $yahooUrl,  $yahooResponse,  $ydata;
    private $gfp, $googleUrl, $googleResponse, $gdata; 
// DECLARE THE OUTPUT DATA
    public $latitude;
    public $longitude;
    public $precision;
    public $warning;
    public $geocoder; 
// DECLARE THE CONSTRUCTOR
    public function __construct()
    {
        $this->latitude  = 0.0;
        $this->longitude = 0.0;
        $this->precision = FALSE; // WANT A VALUE OF 5 OR HIGHER, HIGHER IS BETTER
        $this->warning   = '';
        $this->geocoder  = '';
        unset($this->precis);
    } 
// DECLARE THE DATA-CLEANUP
    private function _cleanup($str)
    {
        $str = ereg_replace('[^\' a-zA-Z0-9&!#$%()"+:?/@,_\.\-]', '', $str);
        return trim(ereg_replace(' +', ' ', $str));
    } 
// DECLARE THE YAHOO! VERSION OF THE WORKHORSE
    public function geocodeYahoo()
    {
        $this->geocoder = 'Yahoo!';
        $yahooUrl       = "http://local.yahooapis.com/MapsService/V1/geocode?&appid=" . YAHOO_API;
        if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
        {
            $yahooUrl .= "&location=" . urlencode($this->_cleanup($this->location));
        }
        else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
        {
            $yahooUrl .= "&street=" . urlencode($this->_cleanup($this->address));
            $yahooUrl .= "&city="   . urlencode($this->_cleanup($this->city));
            $yahooUrl .= "&state="  . urlencode($this->_cleanup($this->state));
            $yahooUrl .= "&zip="    . urlencode($this->_cleanup($this->zip));
        } 
// EXECUTE YAHOO GEOCODER QUERY
// NOTE - USE ERROR SUPPRESSION OR IT WILL BARF UP THE YAHOO API - ON FAILURE RETURNS HTTP 400 BAD REQUEST
        if ($yfp = @fopen($yahooUrl, 'r'))
        {
            $yahooResponse = '';
            while (!feof($yfp)) { $yahooResponse .= fgets($yfp); }
            fclose($yfp);
        }
        else
        {
            return FALSE;
        } 
// EXAMINE THE RESULT
        if ($yahooResponse != '') // NOT EMPTY, WE GOT DATA
        {
            $ydata    = new SimpleXMLElement($yahooResponse); 
// CHECK FOR ANY ERROR MESSAGE, IF NONE, EXTRACT THE DATA POINTS
            $yerror    = $ydata->Message;
            if ($yerror == '')
            {
                $this->precis    = (string)$ydata->Result["precision"];
                $this->warning   = (string)$ydata->Result["warning"];
                $this->latitude  = (string)$ydata->Result->Latitude;
                $this->longitude = (string)$ydata->Result->Longitude; 
// THESE STATEMENTS CAN BE USED TO RETURN NORMALIZED ADDRESS
                $this->address   = (string)$ydata->Result->Address;
                $this->city      = (string)$ydata->Result->City;
                $this->state     = (string)$ydata->Result->State;
                $this->zip       = (string)$ydata->Result->Zip; 
// SET PRECISION TO A NUMBER VALUE
                if ($this->precis == 'zip')     { $this->precision = "5"; }
                if ($this->precis == 'street')  { $this->precision = "6"; }
                if ($this->precis == 'address') { $this->precision = "8"; }
            }
            else
            {
                return FALSE;
            }
        }
        else  // NO RESULT - SOMETHING IS SICK AT YAHOO
        {
            return FALSE;
        }
        return TRUE;
    } // END function geocodeYahoo 
 
// DECLARE THE GOOGLE VERSION OF THE WORKHORSE
    public function geocodeGoogle()
    {
        $this->geocoder = 'Google';
        $googleUrl      = "http://maps.google.com/maps/geo?key=" . GOOGLE_API . "&output=csv"; 
        if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
        {
            $googleUrl .= "&q=" . urlencode($this->_cleanup($this->location));
        }
        else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
        {
            $googleUrl .= "&q="
                       . urlencode(trim(
                         $this->_cleanup($this->address) . ' '
                       . $this->_cleanup($this->city)    . ' '
                       . $this->_cleanup($this->state)   . ' '
                       . $this->_cleanup($this->zip)
                       ));
        } 
// EXECUTE GOOGLE GEOCODER QUERY
        if ($gfp = @fopen($googleUrl, 'r'))
        {
            $googleResponse = '';
            while (!feof($gfp)) { $googleResponse .= fgets($gfp); }
            fclose($gfp);
        }
        else
        {
            return FALSE;
        } 
// EXTRACT THE DATA
        $gdata    = explode(',',$googleResponse);
        if ($gdata[0] != '200') // RESPONSE CODE SHOULD BE '200' -- IF 602 - BAD ZIP CODE OR UNUSABLE ADDRESS
        {
            return FALSE;
        }
        $this->precision = $gdata[1]; // GEOCODE ACCURACY - ZIP CODE = 5, HIGHER NUMBERS ARE BETTER
        $this->latitude  = $gdata[2];
        $this->longitude = $gdata[3];
        return TRUE;
    } // END function geocodeGoogle
} // END class SimpleGeocoder 
 
// A QUICK AND DIRTY FUNCTION TO MAKE THE OUTPUT SHOW THE PUBLIC INFORMATION ONLY
function print_rr($thing)
{
    $str = print_r($thing, TRUE);
    $arr = explode("\n", $str);
    $num = count($arr) -1;
    foreach ($arr as $ptr => $txt)
    {
        if (preg_match('/:private]/', $txt)) { unset($arr[$ptr]); }
    }
    echo implode("\n", $arr);
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:

Select allOpen in new window

 

by: movieprodwPosted on 2009-10-31 at 17:21:27ID: 25712167

This looks great, I will try it out.

Thank you for supplying me with this.

Matt

 

by: Ray_PaseurPosted on 2009-10-31 at 17:22:58ID: 25712171

You're welcome.  IIRC you do not need an API key to run these any more, but if you find you need one, just Google "Geocoder API Key" and the links will probably turn up.

Best regards, ~Ray

 

by: movieprodwPosted on 2009-11-01 at 03:47:41ID: 25713396

This has been very helpful, I was able to correct most of the addresses by running them through your code!

I do have one question, I have attached a code and I was wondering if it was possible for it to spit out the corrected address if it was or was not corrected.

I played with it a bit but ended up reverting most of my changes, I am not good at arrays.

Any help would be great!

<?php // RAY_class_SimpleGeoCoder.php Modified 
$a = '10900 Braemor Dr.';
$c = 'Haslet';
$s = 'tx';
$z = '76052'; 
// A FREEFORM LOCATION STATEMENT IS OPTIONAL - GET ADDRESS, CITY, STATE, ZIP FROM THE URL
$location = '';
if ($a != '') { $location .= $a . ' '; }
if ($c != '') { $location .= $c . ' '; }
if ($s != '') { $location .= $s . ' '; }
if ($z != '') { $location .= $a . ' '; }
$location = trim($location);  
// PREPARE THE GEOCODER
$demo          = new SimpleGeoCoder;
$demo->address = $a;
$demo->city    = $c;
$demo->state   = $s;
$demo->zip     = $z; 
// TEST THE YAHOO! GEOCODER
$demo->geocodeYahoo();
echo "<pre>\n\nYAHOO! ";
print_rr($demo);
echo "</pre>\n";  

// ALL DONE
die(); 
 
// SIMPLE GEOCODER CLASS
class SimpleGeoCoder
{ 
// DECLARE THE INPUT DATA
    public $location; // USE THIS FOR A FREEFORM QUERY, OR USE THE PARTS
    public $address;
    public $city;
    public $state;
    public $zip; 
// DECLARE THE WORKING DATA
    private $precis; // TEXTUAL PRECISION FROM YAHOO
    private $yfp, $yahooUrl,  $yahooResponse,  $ydata;
    private $gfp, $googleUrl, $googleResponse, $gdata; 
// DECLARE THE OUTPUT DATA
    public $latitude;
    public $longitude;
    public $precision;
    public $warning;
    public $geocoder; 
// DECLARE THE CONSTRUCTOR
    public function __construct()
    {
        $this->latitude  = 0.0;
        $this->longitude = 0.0;
        $this->precision = FALSE; // WANT A VALUE OF 5 OR HIGHER, HIGHER IS BETTER
        $this->warning   = '';
        $this->geocoder  = '';
        unset($this->precis);
    } 
// DECLARE THE DATA-CLEANUP
    private function _cleanup($str)
    {
        $str = ereg_replace('[^\' a-zA-Z0-9&!#$%()"+:?/@,_\.\-]', '', $str);
        return trim(ereg_replace(' +', ' ', $str));
    } 
// DECLARE THE YAHOO! VERSION OF THE WORKHORSE
    public function geocodeYahoo()
    {
        $this->geocoder = 'Yahoo!';
        $yahooUrl       = "http://local.yahooapis.com/MapsService/V1/geocode?&appid=" . YAHOO_API;
        if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
        {
            $yahooUrl .= "&location=" . urlencode($this->_cleanup($this->location));
        }
        else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
        {
            $yahooUrl .= "&street=" . urlencode($this->_cleanup($this->address));
            $yahooUrl .= "&city="   . urlencode($this->_cleanup($this->city));
            $yahooUrl .= "&state="  . urlencode($this->_cleanup($this->state));
            $yahooUrl .= "&zip="    . urlencode($this->_cleanup($this->zip));
        } 
// EXECUTE YAHOO GEOCODER QUERY
// NOTE - USE ERROR SUPPRESSION OR IT WILL BARF UP THE YAHOO API - ON FAILURE RETURNS HTTP 400 BAD REQUEST
        if ($yfp = @fopen($yahooUrl, 'r'))
        {
            $yahooResponse = '';
            while (!feof($yfp)) { $yahooResponse .= fgets($yfp); }
            fclose($yfp);
        }
        else
        {
            return FALSE;
        } 
// EXAMINE THE RESULT
        if ($yahooResponse != '') // NOT EMPTY, WE GOT DATA
        {
            $ydata    = new SimpleXMLElement($yahooResponse); 
// CHECK FOR ANY ERROR MESSAGE, IF NONE, EXTRACT THE DATA POINTS
            $yerror    = $ydata->Message;
            if ($yerror == '')
            {
                $this->precis    = (string)$ydata->Result["precision"];
                $this->warning   = (string)$ydata->Result["warning"];
                $this->latitude  = (string)$ydata->Result->Latitude;
                $this->longitude = (string)$ydata->Result->Longitude; 
// THESE STATEMENTS CAN BE USED TO RETURN NORMALIZED ADDRESS
                $this->address   = (string)$ydata->Result->Address;
                $this->city      = (string)$ydata->Result->City;
                $this->state     = (string)$ydata->Result->State;
                $this->zip       = (string)$ydata->Result->Zip; 
                
// SET PRECISION TO A NUMBER VALUE
                if ($this->precis == 'zip')     { $this->precision = "5"; }
                if ($this->precis == 'street')  { $this->precision = "6"; }
                if ($this->precis == 'address') { $this->precision = "8"; }
            }
            else
            {
                return FALSE;
            }
        }
        else  // NO RESULT - SOMETHING IS SICK AT YAHOO
        {
            return FALSE;
        }
        return TRUE;
    } // END function geocodeYahoo 
 
// DECLARE THE GOOGLE VERSION OF THE WORKHORSE
    public function geocodeGoogle()
    {
        $this->geocoder = 'Google';
        $googleUrl      = "http://maps.google.com/maps/geo?key=" . GOOGLE_API . "&output=csv"; 
        if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
        {
            $googleUrl .= "&q=" . urlencode($this->_cleanup($this->location));
        }
        else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
        {
            $googleUrl .= "&q="
                       . urlencode(trim(
                         $this->_cleanup($this->address) . ' '
                       . $this->_cleanup($this->city)    . ' '
                       . $this->_cleanup($this->state)   . ' '
                       . $this->_cleanup($this->zip)
                       ));
        } 
// EXECUTE GOOGLE GEOCODER QUERY
        if ($gfp = @fopen($googleUrl, 'r'))
        {
            $googleResponse = '';
            while (!feof($gfp)) { $googleResponse .= fgets($gfp); }
            fclose($gfp);
        }
        else
        {
            return FALSE;
        } 
// EXTRACT THE DATA
        $gdata    = explode(',',$googleResponse);
        if ($gdata[0] != '200') // RESPONSE CODE SHOULD BE '200' -- IF 602 - BAD ZIP CODE OR UNUSABLE ADDRESS
        {
            return FALSE;
        }
        $this->precision = $gdata[1]; // GEOCODE ACCURACY - ZIP CODE = 5, HIGHER NUMBERS ARE BETTER
        $this->latitude  = $gdata[2];
        $this->longitude = $gdata[3];
        return TRUE;
    } // END function geocodeGoogle
} // END class SimpleGeocoder 
 
// A QUICK AND DIRTY FUNCTION TO MAKE THE OUTPUT SHOW THE PUBLIC INFORMATION ONLY
function print_rr($thing)
{
    $str = print_r($thing, TRUE);
    $arr = explode("\n", $str);
    $num = count($arr) -1;
    foreach ($arr as $ptr => $txt)
    {
        if (preg_match('/:private]/', $txt)) { unset($arr[$ptr]); }
    }
    echo implode("\n", $arr);
}
?>
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-11-01 at 07:02:59ID: 25713878

Sure.  See lines 103 thru 107.  You should be able to access these properties of the SimpleGeoCoder object to get the normalized address.  Then you can compare those fields to the initial values and if they are not equal, the GeoCoder has normalized the address for you.  Normalized addresses are (IIRC) always upper case, so you might want to factor that into your comparisons.

For example, you set the state on line 17.  After calling the SimpleGeoCoder and getting a good answer, you might do something like this:

if (strtoupper($s) == $demo->state) echo "STATE IS NOT CHANGED";

 

by: movieprodwPosted on 2009-11-01 at 08:36:29ID: 25714255

Great, I will start figuring that out. Thanks Ray

 

by: movieprodwPosted on 2009-11-01 at 09:40:25ID: 25714512

Well I am not having any luck, when I plug it in it is saying that there are no items in the rss.

QUOTE:
XML Parsing Error: no element found
Location: http://www.example.com/rss2.php
Line Number 1, Column 1:
END QUOTE

I am plugging in code 1 into code 2 and I get code 3 which does not work, do I have to treat it differently because of the xml encoding?

This is a pain but its really seaming to come together quickly, I did not even know you could do something like this.

I have attached my most advanced code.

Thanks for any pointers

<?php
DEFINE('_ISO','charset=UTF-8'); 
//This program provides google with a structured feed.  It conforms to RSS 2.0 and Googles Namespace 1.0 extensions
//ini_set('display_errors',1);
//error_reporting(E_ALL); 
include('db.php'); 
	//header("Content-Type: text/xml; charset=ISO-8859-1");
	header("Content-Type: text/xml; charset=UTF-8");
	echo getDetails(). getItems();
	
	function getDetails()
	{
		$title = 'DDDD.com, Find your new home. Buy and sell real estate. Search Over 5 Million Listings.';
		$link = 'http://www.DDDD.com';
		$description = 'We are a leading real estate resource with over 5 million properties to choose from. Save time. Maximize your resources. DDDD.com is an incredible tool. Within minutes your property will be ranked atop millions of listings. Join DDDD.com today!';
		
		$details = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>"."\n".
		"<rss version=\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\">"."\n".
		"<channel>"."\n".
		"<title>". $title ."</title>"."\n".
		"<link>". $link ."</link>"."\n".
		"<description>". $description ."</description>"."\n".
		"<language>english</language>"."\n";
			
		return $details;
	}
	
	function getItems()
	{ 
		$query = "SELECT p.*, pt.prop_type_name, lt.listing_type_name, CONCAT(u.first_name, ' ', u.last_name) AS user_wholename
					FROM properties p
					LEFT JOIN users u ON u.userid=p.userid
					LEFT JOIN list_prop_type pt ON pt.prop_type_id=p.prop_type
					LEFT JOIN list_listing_type lt ON lt.listing_type_id=p.listing_type
					WHERE p.active='1'";
		$result = mysql_query ( $query);
		$items = "";
		while($row = mysql_fetch_array($result))
		{
			$description = preg_replace('/[^a-z0-9!, \.]/i', '', $row["prop_desc"]);
			
			
			
			
			
			// START OF RAYS SCRIPT // 
 

			$a = $row["street_address"];
			$c = $row["city"];
			$s = $row["state"];
			$z = $row["zip"]; 
			// A FREEFORM LOCATION STATEMENT IS OPTIONAL - GET ADDRESS, CITY, STATE, ZIP FROM THE URL
			$location = '';
			if ($a != '') { $location .= $a . ' '; }
			if ($c != '') { $location .= $c . ' '; }
			if ($s != '') { $location .= $s . ' '; }
			if ($z != '') { $location .= $a . ' '; }
			$location = trim($location); 
			
			// PREPARE THE GEOCODER
			$demo          = new SimpleGeoCoder;
			$demo->address = $a;
			$demo->city    = $c;
			$demo->state   = $s;
			$demo->zip     = $z; 
			// TEST THE YAHOO! GEOCODER
			$demo->geocodeYahoo();
			print_rr($demo);
			
			
			// ALL DONE
			die(); 
			 
			// SIMPLE GEOCODER CLASS
			class SimpleGeoCoder
			{ 
			// DECLARE THE INPUT DATA
			    public $location; // USE THIS FOR A FREEFORM QUERY, OR USE THE PARTS
			    public $address;
			    public $city;
			    public $state;
			    public $zip; 
			// DECLARE THE WORKING DATA
			    private $precis; // TEXTUAL PRECISION FROM YAHOO
			    private $yfp, $yahooUrl,  $yahooResponse,  $ydata;
			    private $gfp, $googleUrl, $googleResponse, $gdata; 
			// DECLARE THE OUTPUT DATA
			    public $latitude;
			    public $longitude;
			    public $precision;
			    public $warning;
			    public $geocoder; 
			// DECLARE THE CONSTRUCTOR
			    public function __construct()
			    {
			        $this->latitude  = 0.0;
			        $this->longitude = 0.0;
			        $this->precision = FALSE; // WANT A VALUE OF 5 OR HIGHER, HIGHER IS BETTER
			        $this->warning   = '';
			        $this->geocoder  = '';
			        unset($this->precis);
			    } 
			// DECLARE THE DATA-CLEANUP
			    private function _cleanup($str)
			    {
			        $str = ereg_replace('[^\' a-zA-Z0-9&!#$%()"+:?/@,_\.\-]', '', $str);
			        return trim(ereg_replace(' +', ' ', $str));
			    } 
			// DECLARE THE YAHOO! VERSION OF THE WORKHORSE
			    public function geocodeYahoo()
			    {
			        $this->geocoder = 'Yahoo!';
			        $yahooUrl       = "http://local.yahooapis.com/MapsService/V1/geocode?&appid=" . YAHOO_API;
			        if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
			        {
			            $yahooUrl .= "&location=" . urlencode($this->_cleanup($this->location));
			        }
			        else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
			        {
			            $yahooUrl .= "&street=" . urlencode($this->_cleanup($this->address));
			            $yahooUrl .= "&city="   . urlencode($this->_cleanup($this->city));
			            $yahooUrl .= "&state="  . urlencode($this->_cleanup($this->state));
			            $yahooUrl .= "&zip="    . urlencode($this->_cleanup($this->zip));
			        } 
			// EXECUTE YAHOO GEOCODER QUERY
			// NOTE - USE ERROR SUPPRESSION OR IT WILL BARF UP THE YAHOO API - ON FAILURE RETURNS HTTP 400 BAD REQUEST
			        if ($yfp = @fopen($yahooUrl, 'r'))
			        {
			            $yahooResponse = '';
			            while (!feof($yfp)) { $yahooResponse .= fgets($yfp); }
			            fclose($yfp);
			        }
			        else
			        {
			            return FALSE;
			        } 
			// EXAMINE THE RESULT
			        if ($yahooResponse != '') // NOT EMPTY, WE GOT DATA
			        {
			            $ydata    = new SimpleXMLElement($yahooResponse); 
			// CHECK FOR ANY ERROR MESSAGE, IF NONE, EXTRACT THE DATA POINTS
			            $yerror    = $ydata->Message;
			            if ($yerror == '')
			            {
			                $this->precis    = (string)$ydata->Result["precision"];
			                $this->warning   = (string)$ydata->Result["warning"];
			                $this->latitude  = (string)$ydata->Result->Latitude;
			                $this->longitude = (string)$ydata->Result->Longitude; 
			// THESE STATEMENTS CAN BE USED TO RETURN NORMALIZED ADDRESS
			                $thisaddress   = (string)$ydata->Result->Address;
			                $thiscity      = (string)$ydata->Result->City;
			                $thisstate     = (string)$ydata->Result->State;
			                $thiszip       = (string)$ydata->Result->Zip; 
			                $correctedAdd = ''.$thisaddress.', '.$thiscity.', '.$thisstate.', '.$thiszip.', USA';
			                
			// SET PRECISION TO A NUMBER VALUE
			                if ($this->precis == 'zip')     { $this->precision = "5"; }
			                if ($this->precis == 'street')  { $this->precision = "6"; }
			                if ($this->precis == 'address') { $this->precision = "8"; }
			            }
			            else
			            {
			                return FALSE;
			            }
			        }
			        else  // NO RESULT - SOMETHING IS SICK AT YAHOO
			        {
			            return FALSE;
			        }
			        return TRUE;
			    } // END function geocodeYahoo 
			 
			// DECLARE THE GOOGLE VERSION OF THE WORKHORSE
			    public function geocodeGoogle()
			    {
			        $this->geocoder = 'Google';
			        $googleUrl      = "http://maps.google.com/maps/geo?key=" . GOOGLE_API . "&output=csv"; 
			        if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
			        {
			            $googleUrl .= "&q=" . urlencode($this->_cleanup($this->location));
			        }
			        else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
			        {
			            $googleUrl .= "&q="
			                       . urlencode(trim(
			                         $this->_cleanup($this->address) . ' '
			                       . $this->_cleanup($this->city)    . ' '
			                       . $this->_cleanup($this->state)   . ' '
			                       . $this->_cleanup($this->zip)
			                       ));
			        } 
			// EXECUTE GOOGLE GEOCODER QUERY
			        if ($gfp = @fopen($googleUrl, 'r'))
			        {
			            $googleResponse = '';
			            while (!feof($gfp)) { $googleResponse .= fgets($gfp); }
			            fclose($gfp);
			        }
			        else
			        {
			            return FALSE;
			        } 
			// EXTRACT THE DATA
			        $gdata    = explode(',',$googleResponse);
			        if ($gdata[0] != '200') // RESPONSE CODE SHOULD BE '200' -- IF 602 - BAD ZIP CODE OR UNUSABLE ADDRESS
			        {
			            return FALSE;
			        }
			        $this->precision = $gdata[1]; // GEOCODE ACCURACY - ZIP CODE = 5, HIGHER NUMBERS ARE BETTER
			        $this->latitude  = $gdata[2];
			        $this->longitude = $gdata[3];
			        return TRUE;
			    } // END function geocodeGoogle
			} // END class SimpleGeocoder 
			 
			// A QUICK AND DIRTY FUNCTION TO MAKE THE OUTPUT SHOW THE PUBLIC INFORMATION ONLY
			function print_rr($thing)
			{
			    $str = print_r($thing, TRUE);
			    $arr = explode("\n", $str);
			    $num = count($arr) -1;
			    foreach ($arr as $ptr => $txt)
			    {
			        if (preg_match('/:private]/', $txt)) { unset($arr[$ptr]); }
			    }
			    //echo implode("\n", $arr);
			}
			
					
			
			
			// END OF RAYS SCRIPT //
			
			
			
			
			
			
			$items .= "<item>"."\n".
  			//required attributes by Google
			"<title>". htmlentities(strip_tags($row["feat_title"],"ENT_QUOTES")) ."</title>"."\n".
			"<link>http://www.DDDD.com/". $row["propid"] ."</link>"."\n".
			"<description><![CDATA[". urldecode(str_replace("+"," ",$description)) ."]]></description>"."\n".//clean up data
			stringFromPositiveInteger("price",$row["price"],"")."\n".
			stringFromPositiveNumber("bathrooms",$row["baths"],"")."\n".
			stringFromPositiveNumber("bedrooms",$row["beds"],"")."\n".
			"<g:broker>".$row["user_wholename"]."</g:broker>"."\n".
			"<g:id>".$row["propid"]."</g:id>"."\n".
			"<g:listing_status>"."active"."</g:listing_status>"."\n".
			"<g:listing_type>".$row['listing_type_name']."</g:listing_type>"."\n".
			// ADDED THE CORRECTED ADDRESS BELOW //
			"<g:location>".$correctedAdd."</g:location>"."\n".
			"<g:mls_listing_id>".""."</g:mls_listing_id>"."\n".
			"<g:mls_name>".""."</g:mls_name>"."\n".
			"<g:property_type>".$row["prop_type_name"]."</g:property_type>"."\n".
			"<g:provider_class>"."other"."</g:provider_class>"."\n";
			$imagequery = "SELECT * FROM images where propid = ".$row["propid"]." order by mainImage desc";
			$imageresult = mysql_query ( $imagequery);
			$imagecount=0;
			while(($imagerow = mysql_fetch_array($imageresult))&&($imagecount++ < 10))
			{
				$items .= "<g:image_link>"."http://www.DDDD.com/uploads/".$imagerow["img_name"]."</g:image_link>"."\n";
			}
			//optional Google attributes
			$items .= "<g:agent>".""."</g:agent>"."\n".
			stringFromPositiveInteger("area",$row["sq_ft"],"")."\n".
			"<g:feature>".""."</g:feature>"."\n";
			//"<g:expiration_date>".""."</g:expiration_date>"."\n".//YYYY-MM-DD
			//"<g:hoa_dues>".""."</g:hoa_dues>"."\n"; //monthly	
			if($row["sqAcre"]=="Y"){
				$items .=  stringFromPositiveNumber("lot_size",$row["lot_size"]," square ft.")."\n";
			}
			else 
			{
				$items .=  stringFromPositiveNumber("lot_size",$row["lot_size"],"  acre")."\n";
			}
			$items .= "<g:model>".""."</g:model>"."\n".
			//"<g:open_house_date_range>"."<g:start>".""."</g:start>"."<g:end>".""."</g:end>"."</g:open_house_date_range>"."\n".
			stringFromPositiveNumber("property_taxes",'0.00',"")."\n".
			"<g:school>".""."</g:school>"."\n".
			"<g:school_district>".""."</g:school_district>"."\n".
			"<g:style>".""."</g:style>"."\n".
			stringFromPositiveInteger("year",$row["build_year"],"")."\n".
			"<g:zoning>".""."</g:zoning>"."\n"; 
			$items .= "</item>"."\n";
		}
		$items .= "</channel></rss>"."\n";
		return $items;
	}
	
	function checkForPositiveNumber($myNum){
	  return (is_numeric($myNum)&& ($myNum>0));
	}
	function checkForPositiveInteger($myNum){
	  return (ctype_digit($myNum)&& ($myNum>0));
	}
 	function stringFromPositiveInteger($fieldName,$intNum,$unit){
	  //only create string if there is a valid number.  Add unit info if there is any
 	  if(checkForPositiveInteger($intNum)){
      	return "<g:".$fieldName.">".$intNum.$unit."</g:".$fieldName.">";
 	  }
 	  else{
 		return"";
 	  }
 	}
	function stringFromPositiveNumber($fieldName,$Num,$unit){
	  if(checkForPositiveNumber($Num)){
        return "<g:".$fieldName.">".$Num.$unit."</g:".$fieldName.">";
 	  }
 	  else{
 	    return"";
 	  }
 	}
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-11-01 at 13:05:04ID: 25715467

There is nothing in the RSS at all.  Go to that page and then view source - it is empty.  Might want to turn on your error reporting and turn off the header() statements so you can see what the code is doing.

 

by: movieprodwPosted on 2009-11-01 at 13:08:06ID: 25715478

Okay, that is great I could not figure out how to see the errors.

I did that and this is the error:
Fatal error: Class 'SimpleGeoCoder' not found in /var/www/vhosts/example.com/httpdocs/rss2.php on line 69

It is odd because it works fine as a standalone

 

by: Ray_PaseurPosted on 2009-11-01 at 14:02:45ID: 25715791

The code snippet you posted just above this has the class definition embedded in it.  Probably a directory path issue, if the class file was saved somewhere, and your include() statement could not find it.

 

by: movieprodwPosted on 2009-11-01 at 14:19:19ID: 25715861

Okay I will look into it more.

I simply copied and pasted the code into the new piece, its exactly like what I posted, there are no includes.

 

by: Ray_PaseurPosted on 2009-11-01 at 15:07:12ID: 25716097

Fairly sure we are not looking at the code you're using now - the error message from the most recent code snippet could not have occurred on line 69.  Going forward, it's a good idea to make sure that the questions and the posted code always match, otherwise we are just guessing at what might be wrong.

Anyway, this seems to have morphed from a question about verifying physical addresses, which I answered to the best of my ability, into something about RSS and include paths, so I will sign off for now.

Best regards, ~Ray

 

by: movieprodwPosted on 2009-11-01 at 15:09:36ID: 25716108

It is the same code, its line 62

 

by: movieprodwPosted on 2009-11-01 at 15:10:06ID: 31648468

Excellent Job, Thanks!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...