6666 Course Dr 666 , Pompano Beach , FL 33333 Need to split this into streetnumber streetname street type, city, state, zip. Anyone done this before?
The following code will match any address in the format that you posted, minus the commas. I don't know how to make them go onto separate lines though. <?php $txt='6666 Course Dr 666 , Pompano Beach , FL 33333'; $re1='(\\d+)'; # Integer Number 1 $re2='(\\s+)'; # White Space 1 $re3='((?:[a-z][a-z]+))'; # Word 1 $re4='(\\s+)'; # White Space 2 $re5='((?:[a-z][a-z]+))'; # Word 2 $re6='(\\s+)'; # White Space 3 $re7='(\\d+)'; # Integer Number 2 $re8='(\\s+)'; # White Space 4 $re9='.*?'; # Non-greedy match on filler $re10='((?:[a-z][a-z]+))'; # Word 3 $re11='(\\s+)'; # White Space 5 $re12='((?:[a-z][a-z]+))'; # Word 4 $re13='(\\s+)'; # White Space 6 $re14='.*?'; # Non-greedy match on filler $re15='((?:(?:AL)|(?:AK)|(?:AS)|(?:AZ)|(?:AR)|(?:CA)|(?:CO)|(?:CT)|(?:DE)|(?:DC)|(?:FM)|(?:FL)|(?:GA)|(?:GU)|(?:HI)|(?:ID)|(?:IL)|(?:IN)|(?:IA)|(?:KS)|(?:KY)|(?:LA)|(?:ME)|(?:MH)|(?:MD)|(?:MA)|(?:MI)|(?:MN)|(?:MS)|(?:MO)|(?:MT)|(?:NE)|(?:NV)|(?:NH)|(?:NJ)|(?:NM)|(?:NY)|(?:NC)|(?:ND)|(?:MP)|(?:OH)|(?:OK)|(?:OR)|(?:PW)|(?:PA)|(?:PR)|(?:RI)|(?:SC)|(?:SD)|(?:TN)|(?:TX)|(?:UT)|(?:VT)|(?:VI)|(?:VA)|(?:WA)|(?:WV)|(?:WI)|(?:WY)))(?![a-z])'; # US State 1 $re16='(\\s+)'; # White Space 7 $re17='(\\d+)'; # Integer Number 3 if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8.$re9.$re10.$re11.$re12.$re13.$re14.$re15.$re16.$re17."/is", $txt, $matches)) { $int1=$matches[1][0]; $ws1=$matches[2][0]; $word1=$matches[3][0]; $ws2=$matches[4][0]; $word2=$matches[5][0]; $ws3=$matches[6][0]; $int2=$matches[7][0]; $ws4=$matches[8][0]; $word3=$matches[9][0]; $ws5=$matches[10][0]; $word4=$matches[11][0]; $ws6=$matches[12][0]; $usstate1=$matches[13][0]; $ws7=$matches[14][0]; $int3=$matches[15][0]; print "($int1) ($ws1) ($word1) ($ws2) ($word2) ($ws3) ($int2) ($ws4) ($word3) ($ws5) ($word4) ($ws6) ($usstate1) ($ws7) ($int3) \n"; } #----- # Paste the code into a new php file. Then in Unix: # $ php x.php #----- ?> HTML:
Well this accounts for the white space so every white space you could just add a line break? Not sure.
<?php $string = '6666 Course Dr 666 , Pompano Beach , FL 33333'; // match numbers $numberMatch = "([0-9]+)"; // match strings $stringMatch = "([a-zA-Z-_]+)"; // match strings w/spaces $stringMatchSp = "([a-zA-Z-_ ]+)"; // match spaces $spMatch = "\s*"; preg_match("/{$numberMatch}{$spMatch}{$stringMatch}{$spMatch}{$stringMatch}{$spMatch}{$numberMatch}{$spMatch},{$spMatch}{$stringMatchSp}{$spMatch},$spMatch}{$stringMatch}{$spMatch}{$numberMatch}/", $string, $res); echo $res[1]; // Street Number echo $res[2]; // Street Name echo $res[3]; // Street Type echo $res[4]; // Apartment/Suite # echo $res[5]; // City Name echo $res[6]; // State Name echo $res[7]; // Zip Code PHP:
Above code was missing { in the preg_match: preg_match("/{$numberMatch}{$spMatch}{$stringMatch}{$spMatch}{$stringMatch}{$spMatch}{$numberMatch}{$spMatch},{$spMatch}{$stringMatchSp}{$spMatch},{$spMatch}{$stringMatch}{$spMatch}{$numberMatch}/",$string, $res); PHP: