1

I'm getting an address input as one long string as opposed to name, address, city, state and zip. I split most of it up, besides for between the address and the city. So I want to search for any street type name (court, road, street, avenue, etc) and then split the string at the end of the word. Then I will be left with the address and city separate.

strpos() only gives me the position of the beginning of the keyword, I want it to split at the end of the keyword. For example this is where I'm up to:

John Doe
1 Main Street Anytown
NY
00000

I want to split between Street and Anytown. And this address won't be static, there may be more words etc.

Another idea would be a function that automatically splits a string into different fields. Someone told me that in some countries the postal service has an API that does it. Does USPS have such a thing? Their site doesn't indicate it.

Mennyg
  • 330
  • 2
  • 11
  • USPS has API to check weather it is valid address , if you gave an address in correct format. But that doesnot solve the problem here – mujuonly Jul 28 '16 at 08:09
  • You mean strrpos function? (strrpos — Find the position of the last occurrence of a substring in a string) – Maxim Tkach Jul 28 '16 at 08:25
  • 1
    Once you get beginning of keyword with `strpos`, you can just add `strlen("keyword")` to get end (specifically the space after the keyword). – Markus Laire Jul 28 '16 at 08:26
  • @MaximTkach no, i want to find the "t" in street, or the "e" in avenue etc... The last letter position in the string – Mennyg Jul 28 '16 at 08:27
  • seems answered here http://stackoverflow.com/a/3923215/6394185 – Mehmet SÖĞÜNMEZ Jul 28 '16 at 08:36
  • 1
    FWIW, if you're fighting on the level of `strpos`, you're probably gonna lose the larger battle against this problem. You seem to be making very naïve assumptions about what a street address will look like and how it can be split from its city name. That *may* work 80%, 90% of the time, but you *will* fall into false positives with this simple approach. Typically you need an actual address database to resolve against and find the best canonical match from. – deceze Jul 28 '16 at 08:42
  • @deceze are there any solutions like you mention? – Mennyg Jul 28 '16 at 08:45
  • @Mujeebu mentioned the USPS API; unless you're prepared to dive really deep into this topic, I'd suggest an existing API as well (not that I can make any specific recommendations). – deceze Jul 28 '16 at 08:46
  • A more important question is what are you trying to achieve with the address? Even address resolution approaches are still problematic and I don't really have any issues with you using a Bigdata approach. If the core of the problem is address validation, then you should definitely use a library. If however, you're just trying to extrapolate information for storage/matching, then you can go ahead with your current implementation. – Chibueze Opata Jul 28 '16 at 08:47
  • @deceze i think I found something that'll work for me... http://stackoverflow.com/a/10695075/6442152 – Mennyg Jul 28 '16 at 08:51

4 Answers4

1

You could do something like this.

$word = "Street"

$pos = strpos($word, $address);
if ($pos !== false) {
    $pos += strlen($word) - 1;
    ...
}

Depending on how many different words to match there are, using regex might work better than using string functions.

Markus Laire
  • 2,837
  • 2
  • 17
  • 23
1

Actually what you should do is searching for multiple needle in haystack, summing position and length of needle.

function strpos_street($string){
    $types = [
   'court', 
   'road', 
   'street', 
   'avenue' 
  ];
  foreach($types as $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 a same iterative way
  • I'm not sure but there may be some addresses which contains both "street" and "avenue" words
Tuğca Eker
  • 1,493
  • 13
  • 20
0

It seems that you're trying to search in reverse as strpos gives position of a string starting from the beginning by default, you will need to set the offset to -1 and subtract from strlen.

i.e.

strlen($address) - strpos($address, 'Anytown', -1) - 1;
//search from reverse and output position starting from reverse

However as pointed out in the comments, this may not the best approach for the problem you're trying to sovle.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
0

You can use explode function, which split a string using keyword and return an array of strings. Example of using explode:

$str = '1 Main Street Anytown';        
$array_of_string = explode('street',$str);    

Then $array_of_strings[0] will contains 1 Main and $array_of_strings[1] will contains Anytown

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
A.Tarhini
  • 72
  • 4