1

我正在我的网站上实现基于位置的搜索,该搜索询问一个开放式问题,location?即用户输入的文本字段。

用户可以输入任何内容的组合,例如城市、州、邮编、城市、州、邮编,甚至是完整地址,例如123 elm st Dallas, TX 75206

最大的挑战来自用户输入的差异。例如,一些城市的名称中有空格或 hifen,例如San Diego.

有些人输入地址不同。Dallas, TX 75206Dallas TX 75206Dallas, TX, 75206Dallas, TX。输入地址的方法有很多。

我想我想知道是否有一个通用的正则表达式、方法或公式用于分解这些部分并将它们匹配到正确的部分。我不是想重新发明轮子,这似乎是一个普遍的问题。

4

2 回答 2

0

所以我基本上把每一部分都分解了,并使用逻辑来尝试分割大多数用户可以/应该使用的参数。

(这是使用 CakePHP)

<?php
    $addrString = $this->request->query['location'];
    //converto the address string to uppercase to match against the city and state arrays
    $addrString = strtoupper($addrString);

    //Convert address to array to look for a zip code
    $addrArray = preg_split('/[;,. \n]+/', $addrString);

    //List of cities in Oregon and Washington
    $cities = array('ABERDEEN','ABERDEEN GARDENS','ACME','AHTANUM','AIRWAY HEIGHTS','ALBION','ALDERWOOD MANOR','ALGER','ALGONA','ALLYN-GRAPEVIEW','ALMIRA','AMBOY','AMES LAKE','ANACORTES','ARLINGTON','ARLINGTON HEIGHTS','ARTONDALE','ASHFORD','ASOTIN','AUBURN','AULT FIELD','BAINBRIDGE ISLAND','BANGOR TRIDENT BASE','BANKS LAKE SOUTH','BARBERTON','BARING','BASIN CITY','BATTLE GROUND','BAY CENTER','BAY VIEW','BEAUX ARTS VILLAGE','BELLEVUE','BELL HILL','BELLINGHAM','BENTON CITY','BICKLETON','BIG LAKE','BINGEN','BIRCH BAY','BLACK DIAMOND','BLAINE','BLYN','BONNEY LAKE','BOTHELL','BRADY','BREMERTON','BREWSTER','BRIDGEPORT','BRIER','BRINNON','BRUSH PRAIRIE','BRYN MAWR-SKYWAY','BUCKLEY','BUCODA','BURBANK','BURIEN','BURLINGTON','CAMANO','CAMAS','CARBONADO','CARLSBORG','CARNATION','CARSON RIVER VALLEY','CASCADE-FAIRWOOD','CASCADE VALLEY','CASHMERE','CASTLE ROCK','CATHAN','CATHCART','CATHLAMET','CENTERVILLE','CENTRALIA','CENTRAL PARK','CHEHALIS','CHEHALIS VILLAGE','CHELAN','CHENEY','CHERRY GROVE','CHEWELAH','CHINOOK','CLARKSTON','CLARKSTON HEIGHTS-VINELAND','CLEAR LAKE','CLE ELUM','CLINTON','CLYDE HILL','COHASSETT BEACH','COLFAX','COLLEGE PLACE','COLTON','COLVILLE','CONCONULLY','CONCRETE','CONNELL','CONWAY','COPALIS BEACH','COSMOPOLIS','COTTAGE LAKE','COULEE CITY','COULEE DAM','COUNTRY HOMES','COUPEVILLE','COVINGTON','CRESTON','CUSICK','CUSTER','DALLESPORT','DARRINGTON','DAVENPORT','DAYTON','DEER PARK','DEMING','DESERT AIRE','DES MOINES','DISHMAN','DIXIE','DOLLAR CORNER','DUPONT','DUVALL','EAST CATHLAMET','EASTGATE','EAST HILL-MERIDIAN','EASTON','EAST PORT ORCHARD','EAST RENTON HIGHLANDS','EAST WENATCHEE','EAST WENATCHEE BENCH','EATONVILLE','ECHO LAKE','EDGEWOOD','EDISON','EDMONDS','ELBE','ELECTRIC CITY','ELK PLAIN','ELLENSBURG','ELMA','ELMER CITY','ENDICOTT','ENTIAT','ENUMCLAW','EPHRATA','ERLANDS POINT-KITSAP LAKE','ESCHBACH','ESPERANCE','EVERETT','EVERSON','FAIRCHILD AFB','FAIRFIELD','FAIRWOOD','FALL CITY','FARMINGTON','FEDERAL WAY','FELIDA','FERNDALE','FIFE','FINLEY','FIRCREST','FIVE CORNERS','FORDS PRAIRIE','FORKS','FORT LEWIS','FOX ISLAND','FREDERICKSON','FREELAND','FRIDAY HARBOR','GARFIELD','GARRETT','GENEVA','GEORGE','GIG HARBOR','GLACIER','GLEED','GOLD BAR','GOLDENDALE','GRAHAM','GRAND COULEE','GRAND MOUND','GRANDVIEW','GRANGER','GRANITE FALLS','GRAYLAND','GREEN ACRES','GREENWATER','HAMILTON','HARRAH','HARRINGTON','HARTLINE','HATTON','HAZEL DELL NORTH','HAZEL DELL SOUTH','HIGHLAND','HOBART','HOCKINSON','HOQUIAM','HUMPTULIPS','HUNTS POINT','ILWACO','INCHELIUM','INDEX','INDIANOLA','INGLEWOOD-FINN HILL','IONE','ISSAQUAH','JOHN SAM LAKE','JORDAN ROAD-CANYON CREEK','JUNCTION CITY','KAHLOTUS','KALAMA','KELSO','KENDALL','KENMORE','KENNEWICK','KENT','KETTLE FALLS','KINGSGATE','KINGSTON','KIRKLAND','KITTITAS','KLICKITAT','KRUPP','LA CENTER','LACEY','LA CONNER','LA CROSSE','LAKE BOSWORTH','LAKE CAVANAUGH','LAKE FOREST PARK','LAKE GOODWIN','LAKE KETCHUM','LAKELAND NORTH','LAKELAND SOUTH','LAKE MCMURRAY','LAKE MARCEL-STILLWATER','LAKE MORTON-BERRYDALE','LAKE ROESIGER','LAKE SHORE','LAKE STEVENS','LAKEVIEW','LAKEWOOD','LAMONT','LANGLEY','LATAH','LEA HILL','LEAVENWORTH','LEBAM','LEWISVILLE','LIBERTY LAKE','LIND','LOCHSLOY','LONG BEACH','LONGVIEW','LONGVIEW HEIGHTS','LYLE','LYMAN','LYNDEN','LYNNWOOD','MABTON','MCCHORD AFB','MCCLEARY','MACHIAS','MALDEN','MALONE-PORTER','MALTBY','MANCHESTER','MANSFIELD','MAPLE FALLS','MAPLE HEIGHTS-LAKE DESIRE','MAPLE VALLEY','MARBLEMOUNT','MARCUS','MARIETTA-ALDERWOOD','MARKHAM','MARROWSTONE','MARTHA LAKE','MARYHILL','MARYSVILLE','MATTAWA','MAY CREEK','MEADOW GLADE','MEDICAL LAKE','MEDINA','MERCER ISLAND','MESA','METALINE','METALINE FALLS','MIDLAND','MILL CREEK','MILL PLAIN','MILLWOOD','MILTON','MINNEHAHA','MIRRORMONT','MOCLIPS','MONROE','MONTESANO','MORTON','MOSES LAKE','MOSES LAKE NORTH','MOSSYROCK','MOUNTLAKE TERRACE','MOUNT VERNON','MOUNT VISTA','MOXEE','MUKILTEO','NACHES','NAPAVINE','NASELLE','NAVY YARD CITY','NEAH BAY','NEILTON','NESPELEM','NESPELEM COMMUNITY','NEWCASTLE','NEWPORT','NISQUALLY INDIAN COMMUNITY','NOOKSACK','NORMANDY PARK','NORTH BEND','NORTH BONNEVILLE','NORTH CREEK','NORTH MARYSVILLE','NORTH OMAK','NORTHPORT','NORTH STANWOOD','NORTH SULTAN','NORTHWEST SNOHOMISH','NORTH YELM','OAKESDALE','OAK HARBOR','OAKVILLE','OCEAN CITY','OCEAN PARK','OCEAN SHORES','ODESSA','OKANOGAN','OLYMPIA','OMAK','OPPORTUNITY','ORCHARDS','OROVILLE','ORTING','OSO','OTHELLO','OTIS ORCHARDS-EAST FARMS','OYEHUT-HOGANS CORNER','PACIFIC','PAINE FIELD-LAKE STICKNEY','PALOUSE','PARKLAND','PARKWOOD','PASCO','PATEROS','PEACEFUL VALLEY','PE ELL','PICNIC POINT-NORTH LYNNWOOD','POMEROY','PORT ANGELES','PORT ANGELES EAST','PORT HADLOCK-IRONDALE','PORT LUDLOW','PORT ORCHARD','PORT TOWNSEND','POULSBO','PRAIRIE RIDGE','PRESCOTT','PRIEST POINT','PROSSER','PULLMAN','PUYALLUP','QUILCENE','QUINCY','RAINIER','RAVENSDALE','RAYMOND','REARDAN','REDMOND','RENTON','REPUBLIC','RICHLAND','RIDGEFIELD','RITZVILLE','RIVERBEND','RIVER ROAD','RIVERSIDE','RIVERTON-BOULEVARD PARK','ROCHESTER','ROCKFORD','ROCK ISLAND','ROCKPORT','RONALD','ROOSEVELT','ROSALIA','ROSLYN','ROY','ROYAL CITY','RUSTON','ST. JOHN','SALMON CREEK','SAMMAMISH','SATSOP','SATUS','SEATAC','SEATTLE','SEATTLE HILL-SILVER FIRS','SEDRO-WOOLLEY','SELAH','SEQUIM','SHAKER CHURCH','SHELTON','SHORELINE','SILVANA','SILVERDALE','SKOKOMISH','SKYKOMISH','SMOKEY POINT','SNOHOMISH','SNOQUALMIE','SNOQUALMIE PASS','SOAP LAKE','SOUTH BEND','SOUTH CLE ELUM','SOUTH HILL','SOUTH PRAIRIE','SOUTH WENATCHEE','SPANAWAY','SPANGLE','SPOKANE','SPRAGUE','SPRINGDALE','STANWOOD','STARBUCK','STARTUP','STEILACOOM','STEVENSON','STIMSON CROSSING','SUDDEN VALLEY','SULTAN','SUMAS','SUMMIT','SUMMITVIEW','SUMNER','SUNNYSIDE','SUNNYSLOPE','SUQUAMISH','TACOMA','TAHOLAH','TANGLEWILDE-THOMPSON PLACE','TANNER','TEKOA','TENINO','TERRACE HEIGHTS','THORP','THREE LAKES','TIETON','TOKELAND','TOLEDO','TONASKET','TOPPENISH','TOUCHET','TOWN AND COUNTRY','TRACYTON','TRENTWOOD','TROUT LAKE','TUKWILA','TULALIP BAY','TUMWATER','TWISP','UNION GAP','UNION HILL-NOVELTY HILL','UNIONTOWN','UNIVERSITY PLACE','VADER','VANCOUVER','VANTAGE','VASHON','VENERSBORG','VERADALE','VERLOT','WAITSBURG','WALLA WALLA','WALLA WALLA EAST','WALLER','WALLULA','WALNUT GROVE','WAPATO','WARDEN','WARM BEACH','WASHOUGAL','WASHTUCNA','WATERVILLE','WAVERLY','WEALLUP LAKE','WENATCHEE','WEST CLARKSTON-HIGHLAND','WEST LAKE SAMMAMISH','WEST LAKE STEVENS','WEST LONGVIEW','WEST PASCO','WESTPORT','WEST RICHLAND','WEST SIDE HIGHWAY','WEST VALLEY','WEST WENATCHEE','WHITE CENTER','WHITE SALMON','WHITE SWAN','WILBUR','WILKESON','WILSON CREEK','WINLOCK','WINTHROP','WISHRAM','WOODINVILLE','WOODLAND','WOODS CREEK','WOODWAY','YACOLT','YAKIMA','YARROW POINT','YELM','ZILLAH','ADAIR VILLAGE','ADAMS','ADRIAN','ALBANY','ALOHA','ALTAMONT','AMITY','ANTELOPE','ARLINGTON','ASHLAND','ASTORIA','ATHENA','AUMSVILLE','AURORA','BAKER CITY','BANDON','BANKS','BARLOW','BARVIEW','BAY CITY','BEAVER','BEAVERTON','BEND','BIGGS JUNCTION','BOARDMAN','BONANZA','BROOKINGS','BROOKS','BROWNSVILLE','BUNKER HILL','BURNS','BUTTE FALLS','BUTTEVILLE','CANBY','CANNON BEACH','CANYON CITY','CANYONVILLE','CAPE MEARES','CARLTON','CASCADE LOCKS','CAVE JUNCTION','CAYUSE','CEDAR HILLS','CEDAR MILL','CENTRAL POINT','CHENOWETH','CHILOQUIN','CITY OF THE DALLES','CLACKAMAS','CLATSKANIE','CLOVERDALE','COBURG','COLUMBIA CITY','CONDON','COOS BAY','COQUILLE','CORNELIUS','CORVALLIS','COTTAGE GROVE','COVE','CRESWELL','CULVER','DALLAS','DAYTON','DAYVILLE','DEPOE BAY','DESCHUTES RIVER WOODS','DETROIT','DONALD','DRAIN','DUFUR','DUNDEE','DUNES CITY','DURHAM','EAGLE POINT','ECHO','ELGIN','ELKTON','ENTERPRISE','EOLA','ESTACADA','EUGENE','FAIRVIEW','FALLS CITY','FLORENCE','FOREST GROVE','FOSSIL','FOUR CORNERS','GARDEN HOME-WHITFORD','GARIBALDI','GASTON','GATES','GEARHART','GERVAIS','GLADSTONE','GLENDALE','GLIDE','GOLD BEACH','GOLD HILL','GOPHER FLATS','GRAND RONDE','GRANITE','GRANTS PASS','GRASS VALLEY','GREEN','GREENHORN','GRESHAM','HAINES','HALFWAY','HALSEY','HAPPY VALLEY','HARBECK-FRUITDALE','HARBOR','HARRISBURG','HAYESVILLE','HEBO','HELIX','HEPPNER','HERMISTON','HILLSBORO','HINES','HOOD RIVER','HUBBARD','HUNTINGTON','IDANHA','IMBLER','INDEPENDENCE','IONE','IRRIGON','ISLAND CITY','JACKSONVILLE','JEFFERSON','JENNINGS LODGE','JOHN DAY','JOHNSON CITY','JORDAN VALLEY','JOSEPH','JUNCTION CITY','KEIZER','KING CITY','KIRKPATRICK','KLAMATH FALLS','LABISH VILLAGE','LAFAYETTE','LA GRANDE','LAKE OSWEGO','LAKESIDE','LAKEVIEW','LA PINE','LEBANON','LEXINGTON','LINCOLN BEACH','LINCOLN CITY','LONEROCK','LONG CREEK','LOSTINE','LOWELL','LYONS','MCMINNVILLE','MADRAS','MALIN','MANZANITA','MARION','MAUPIN','MAYWOOD PARK','MEDFORD','MEHAMA','MERRILL','METOLIUS','METZGER','MILL CITY','MILLERSBURG','MILTON-FREEWATER','MILWAUKIE','MISSION','MITCHELL','MOLALLA','MONMOUTH','MONROE','MONUMENT','MORO','MOSIER','MOUNT ANGEL','MOUNT HOOD VILLAGE','MOUNT VERNON','MYRTLE CREEK','MYRTLE POINT','NEHALEM','NESKOWIN','NETARTS','NEWBERG','NEWPORT','NORTH BEND','NORTH PLAINS','NORTH POWDER','NYSSA','OAK GROVE','OAK HILLS','OAKLAND','OAKRIDGE','OATFIELD','OCEANSIDE','ODELL','ONTARIO','OREGON CITY','PACIFIC CITY','PAISLEY','PARKDALE','PENDLETON','PHILOMATH','PHOENIX','PILOT ROCK','PINE GROVE','PINE HOLLOW','PORTLAND','PORT ORFORD','POWERS','PRAIRIE CITY','PRESCOTT','PRINEVILLE','RAINIER','RALEIGH HILLS','REDMOND','REDWOOD','REEDSPORT','RICHLAND','RICKREALL','RIDDLE','RIVERGROVE','RIVERSIDE','ROCKAWAY BEACH','ROCKCREEK','ROGUE RIVER','ROSEBURG','ROSEBURG NORTH','ROSE LODGE','ROWENA','RUFUS','ST. HELENS','ST. PAUL','SALEM','SANDY','SCAPPOOSE','SCIO','SCOTTS MILLS','SEASIDE','SENECA','SHADY COVE','SHANIKO','SHERIDAN','SHERWOOD','SILETZ','SILVERTON','SISTERS','SODAVILLE','SOUTH LEBANON','SPRAY','SPRINGFIELD','STANFIELD','STAYTON','SUBLIMITY','SUMMERVILLE','SUMPTER','SUNNYSIDE','SUTHERLIN','SWEET HOME','TALENT','TANGENT','TERREBONNE','THREE RIVERS','TIGARD','TILLAMOOK','TOLEDO','TRI-CITY','TROUTDALE','TUALATIN','TURNER','TUTUILLA','TYGH VALLEY','UKIAH','UMATILLA','UNION','UNITY','VALE','VENETA','VERNONIA','WALDPORT','WALLOWA','WAMIC','WARM SPRINGS','WARRENTON','WASCO','WATERLOO','WESTFIR','WEST HAVEN-SYLVAN','WEST LINN','WESTON','WEST SLOPE','WHEELER','WHITE CITY','WILLAMINA','WILSONVILLE','WINCHESTER BAY','WINSTON','WOODBURN','WOOD VILLAGE','YACHATS','YAMHILL','YONCALLA'); 
    //These cities appear both in Oregon and Washington
    $duplicateCities = array('ARLINGTON', 'BEAVER', 'BRIDGEPORT', 'CARLTON', 'COLTON', 'DAYTON', 'IONE', 'KENT');
    //Variations of State spellings/abbreviations
    $states = array('WA', 'WASH', 'WASHINGTON', 'OR', 'ORE', 'OREGON');

    //Grab last item in address array, take first 5 characters
    $possibleZip = end($addrArray);
    $possibleZip = substr($possibleZip, 0, 5);
    //See if it looks like it could be a zipcode
    if(preg_match('/^\d{5}$/', $possibleZip)){
        //If the last part of the string looks like it could be a zip code
        //use that zip as the zip code to use for the search";  
        //$zip_to_search = $possibleZip;
        $conditions[] = array('Listing.ZipCode' => $possibleZip); 
    }else{
        //echo "Do a search of comparison of the addr_string array against the cities array to see if a city can be found and searched for. If not, return nothing?<br />";  
        //Check to see if the address string contains a city in WA or OR
        $city = $this->contains($addrString, $cities);
        $city = end($city);

        $state = array_intersect($addrArray, $states);
        $state = substr(end($state), 0, 2);

        if($city){
            if( in_array($city, $duplicateCities)){
            //Check to see if there's a duplicate city, 
            //If there is you'll have to do a search on both City and State
                if($state){
                    //$addr_conditions[] = array('Zip.state_prefix' => $state); 
                    //$addr_conditions[] = array('Zip.City' => $city); 
                    //$addr_state = $this->Zip->find('first', array('conditions' => $addr_conditions));
                    //$zip_to_search = $addr_state['Zip']['zip_code'];

                    $conditions[] = array('Listing.City' => $city); 
                    $conditions[] = array('Listing.State' => $state);
                }
            }else{
            //If no duplicate city was found above, just do a search on the city provided
                //$addr_city = $this->Zip->findByCity($city);
                //$zip_to_search = $addr_city['Zip']['zip_code'];
                $conditions[] = array('Listing.City' => $city); 
            }

        }elseif($state){
            //$addr_conditions[] = array('Zip.state_prefix' => $state); 
            //$addr_conditions[] = array('Zip.City' => $city); 
            //$addr_state = $this->Zip->find('first', array('conditions' => $addr_conditions));
            //$zip_to_search = $addr_state['Zip']['zip_code'];

            $conditions[] = array('Listing.State' => $state);
        }
    }
?>
于 2013-07-01T19:44:06.653 回答
0

做就是了:

var type;

if (postcode == true) {
    type = postcode;
} else {
    type = notAPostCode;
}

return type;
于 2013-04-24T15:58:15.593 回答