1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need Regex help to split a street address

Discussion in 'PHP' started by blake3334, Feb 19, 2012.

  1. #1
    6666 Course Dr 666 , Pompano Beach , FL 33333 Need to split this into streetnumber streetname street type, city, state, zip.

    Anyone done this before?
     
    blake3334, Feb 19, 2012 IP
  2. RadioBounce

    RadioBounce Banned

    Messages:
    4,171
    Likes Received:
    16
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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:
     
    Last edited by a moderator: Feb 19, 2012
    RadioBounce, Feb 19, 2012 IP
  3. blake3334

    blake3334 Member

    Messages:
    407
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    wow thats looks messy, I doubt it's possible to get it into one regex and use preg_split?
     
    blake3334, Feb 19, 2012 IP
  4. RadioBounce

    RadioBounce Banned

    Messages:
    4,171
    Likes Received:
    16
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Well this accounts for the white space so every white space you could just add a line break? Not sure.
     
    RadioBounce, Feb 19, 2012 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    
    <?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:
     
    ThePHPMaster, Feb 19, 2012 IP
  6. blake3334

    blake3334 Member

    Messages:
    407
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    Seems not to work. Anyone else? Anything simpler?
     
    blake3334, Feb 19, 2012 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    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:
     
    ThePHPMaster, Feb 19, 2012 IP
  8. blake3334

    blake3334 Member

    Messages:
    407
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #8
    NVM Works, thanks!
     
    Last edited: Feb 19, 2012
    blake3334, Feb 19, 2012 IP