<?php
/*
    Location Parser v1.01, 2005-04-17
    Copyright 2005 Michael Boone
    mike@Xboonedocks.net (remove the X)
    http://boonedocks.net/

    Provided under the GNU General Public License
    Contact me for use outside the bounds of that license

    ---------------------------------------------------------------    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    The GNU General Public License can be found at:
    http://www.gnu.org/copyleft/gpl.html
    ---------------------------------------------------------------    
    
*/
    
function findLocation($strLocation) {
    
// given a free-form location string, determine two character state/province code and return it
    // currently United States and Canada only
    // returns an empty string if a location cannot be determined

    // define state/province array as static so we only define it once
    // generally I put these in alphabetical order, but I have moved certain states/provinces with
    // variations that may appear in other words to the end
    // final period is omitted from abbreviations since the period is a regex word boundary
    // periods in the middle like N.C. get kept, i.e. N.C. becomes N.C
    
static $aryStates=array(
        array(
abr=>'AL',poss=>'Alabama;AL;Ala'),
        array(
abr=>'AK',poss=>'Alaska;AK'),
        array(
abr=>'AZ',poss=>'Arizona;AZ;Ariz'),
        array(
abr=>'AR',poss=>'Arkansas;AR;Ark'),
        array(
abr=>'CA',poss=>'California;CA;Cal.;Calif'),
        array(
abr=>'CT',poss=>'Connecticut;CT;Conn'),
        array(
abr=>'DC',poss=>'District of Columbia;DC;D.C'),
        array(
abr=>'FL',poss=>'Florida;FL;Fla'),
        array(
abr=>'GA',poss=>'Georgia;GA'),
        array(
abr=>'HI',poss=>'Hawaii;HI'),
        array(
abr=>'ID',poss=>'Idaho;ID'),
        array(
abr=>'IL',poss=>'Illinois;IL;Ill'),
        array(
abr=>'IA',poss=>'Iowa;IA'),
        array(
abr=>'KS',poss=>'Kansas;KS;Kan'),
        array(
abr=>'KY',poss=>'Kentucky;KY'),
        array(
abr=>'ME',poss=>'Maine;ME'),
        array(
abr=>'MD',poss=>'Maryland;MD'),
        array(
abr=>'MA',poss=>'Massachusetts;MA;Mass'),
        array(
abr=>'MI',poss=>'Michigan;MI;Mich'),
        array(
abr=>'MN',poss=>'Minnesota;MN;Minn'),
        array(
abr=>'MS',poss=>'Mississippi;MS;Miss'),
        array(
abr=>'MO',poss=>'Missouri;MO'),
        array(
abr=>'MT',poss=>'Montana;MT;Mont'),
        array(
abr=>'NE',poss=>'Nebraska;NE;Neb'),
        array(
abr=>'NV',poss=>'Nevada;NV;Nev'),
        array(
abr=>'NH',poss=>'New Hampshire;NH;N.H'),
        array(
abr=>'NJ',poss=>'New Jersey;NJ;N.J'),
        array(
abr=>'NM',poss=>'New Mexico;NM;N.M'),
        array(
abr=>'NY',poss=>'New York;NY;N.Y'),
        array(
abr=>'NC',poss=>'North Carolina;NC;N. Carolina;N.C'),
        array(
abr=>'ND',poss=>'North Dakota;ND;N.D'),
        array(
abr=>'OH',poss=>'Ohio;OH'),
        array(
abr=>'OK',poss=>'Oklahoma;OK;Okla'),
        array(
abr=>'OR',poss=>'Oregon;OR;Ore'),
        array(
abr=>'PA',poss=>'Pennsylvania;PA;Penn'),
        array(
abr=>'RI',poss=>'Rhode Island;RI;R.I'),
        array(
abr=>'SC',poss=>'South Carolina;SC;S. Carolina;S.C'),
        array(
abr=>'SD',poss=>'South Dakota;SD;S. Dakota;S.D'),
        array(
abr=>'TN',poss=>'Tennessee;TN;Tenn'),
        array(
abr=>'TX',poss=>'Texas;TX;Tex'),
        array(
abr=>'UT',poss=>'Utah;UT'),
        array(
abr=>'VT',poss=>'Vermont;VT'),
        array(
abr=>'WA',poss=>'Washington;WA;Wash'),
        array(
abr=>'WV',poss=>'West Virginia;WV;W. Virginia;W. Va'),
        array(
abr=>'VA',poss=>'Virginia;VA'),
        array(
abr=>'WI',poss=>'Wisconsin;WI;Wis.;Wisc'),
        array(
abr=>'WY',poss=>'Wyoming;WY;Wyo'),
        array(
abr=>'AB',poss=>'Alberta;AB;Alb'),
        array(
abr=>'BC',poss=>'British Columbia;BC;B.C'),
        array(
abr=>'MB',poss=>'Manitoba;MB'),
        array(
abr=>'NB',poss=>'New Brunswick;NB'),
        array(
abr=>'NL',poss=>'Newfoundland;Labrador;NL'),
        array(
abr=>'NT',poss=>'Northwest Territories;NT'),
        array(
abr=>'NS',poss=>'Nova Scotia;NS'),
        array(
abr=>'PE',poss=>'Prince Edward Island;PE'),
        array(
abr=>'QC',poss=>'Quebec;QC;PQ'),
        array(
abr=>'SK',poss=>'Saskatchewan;SK'),
        array(
abr=>'YT',poss=>'Yukon Territory;YT'),
        array(
abr=>'NU',poss=>'Nunavut;NU'),
        array(
abr=>'CO',poss=>'Colorado;CO;Colo'),
        array(
abr=>'ON',poss=>'Ontario;ON;Ont'),
        array(
abr=>'IN',poss=>'Indiana;IN;Ind'),
        array(
abr=>'LA',poss=>'Louisiana;LA'),
        array(
abr=>'DE',poss=>'Delaware;DE;Del')
        );
    
    
$strReturn='';
    
$strLocation=trim($strLocation);
    
    if (
$strLocation!='') {
        foreach (
$aryStates as $ary1State) {
            
$aryTest=explode(';',$ary1State['poss']);
            
            foreach (
$aryTest as $strTest) {
                
// if a match, return the abbreviation and exit out of the regex testing
                
$strTest=str_replace('.','\.',$strTest);
                if (
preg_match('/^.*\b' $strTest '\b.*$/i'$strLocation)) {
                    
$strReturn=$ary1State['abr'];
                    break 
2;
                }
            }
        }
    }
    
    return 
$strReturn;
}

?>