php - How to find the end of a word using strpos() -


i'm getting address input 1 long string opposed name, address, city, state , zip. split of up, besides between address , city. want search street type name (court, road, street, avenue, etc) , split string @ end of word. left address , city separate.

strpos() gives me position of beginning of keyword, want split @ end of keyword. example i'm to:

john doe 1 main street anytown ny 00000 

i want split between street , anytown. , address won't static, there may more words etc.

another idea function automatically splits string different fields. told me in countries postal service has api it. usps have such thing? site doesn't indicate it.

actually should searching multiple needle in haystack, summing position , length of needle.

function strpos_street($string){     $types = [    'court',     'road',     'street',     'avenue'    ];   foreach($types $t){       $pos = strpos(strtolower($string), strtolower($t));       if($pos === false) continue;       else return $pos + strlen($t) - 1;   }   return -1; }  echo strpos_street($string); 

some tips:

  • consider case sensivity
  • you may use explode function in same iterative way
  • i'm not sure there may addresses contains both "street" , "avenue" words

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -