fork(4) download
  1. <?php
  2. //posted address
  3. $address = "str main one bldg 5b other param";
  4. //to replace
  5. $replace = ['street'=>['str','st'],
  6. 'building'=>['bldg','bld'],
  7. 'number'=>['nmbr','numb','nr',]];
  8. //replace
  9. foreach($replace as $field=>$abbrs)
  10. foreach($abbrs as $abbr)
  11. $address = str_replace($abbr." ",$field." ",$address);
  12. //fields
  13. $fields = array_keys($replace);
  14. //match
  15. if(preg_match_all('/('.implode('|',$fields).')\s+([^\s]+)/si', $address, $matches)) {
  16. //matches
  17. $search = array_combine($matches[1], $matches[2]);
  18. //other
  19. $search['other'] = str_replace($matches[0],"",$address);
  20. }else{
  21. //search in all the fields
  22. $search['other'] = $address;
  23. }
  24. //search
  25. print_r($search);
  26.  
Success #stdin #stdout 0s 82944KB
stdin
Standard input is empty
stdout
Array
(
    [street] => main
    [building] => 5b
    [other] =>  one  other param
)