Raj
Raj

Reputation: 1

Get the substring from the first number to the next occurring number after a minimum number of characters

I have a string like this, which I need to extract the address from:

$string="xyz company 7 th floor hotel yyyy 88 main Road mumbai 400000 this is sample  comapny address 9456  and some other";

$word=str_word_count($string,1,'0...9');

Now word has each word like word[0]=xyz, word[1]=company, word[2]=7, etc.

I need to compare each value. If the word is a number then I want to save it in a temp variable until I get another number.
For example word[2] is 7, so I need to save the values from then until 88 in a temp variable. So the temp should contain "7 th floor hotel yyyy 88".

If the temp variable has fewer than 25 characters then we compare until we get another number. So here we need to keep going from 88 to 400000 and append that to the temp variable.

The temp should finally look like this: "7 th floor hotel yyyy 88 main Road mumbai 400000"

Any help please?

Upvotes: 0

Views: 388

Answers (2)

cletus
cletus

Reputation: 625087

Try using preg_match_all():

if (preg_match_all('!(?<=\b)\d\b+.*\b+\d+(?<=\b)!', $string, $matches)) {
  echo $matches[0][0];
}

What this is doing is testing for a sequence of numbers followed by any number of characters followed by another sequence of numbers. The expressions are greedy so the middle pattern (.*) should grab as many as possible meaning you'll be grabbing from the first to the last sets of digits.

There is a lookahead and lookbehind in there to check to see if the numbers are on word boundaries. You may or may not need this and you may or may not need to tweak it depending on your exact requirements.

The above works on the whole string.

If you must (or just prefer) to operate on the words:

$start = false;
$last = false;
$i = 0;
foreach ($words as $word) {
  if (is_numeric($word)) {
    if ($start === false) {
      $start = $i;
    }
    $last = $i;
  }
  $i++;
}
$word_range = $words;
array_splice($word_range, $start, $last - $start + 1);
$substring = implode(' ', $word_range);

Upvotes: 0

Steven
Steven

Reputation: 18014

The question was already asked here, where I responded. Although preg_match does not follow your thought process, it accomplishes the result you're looking for. The only change you've made between that question and this one is the 25 character restriction. This can easily be resolved by accepting 25 characters of any type before checking for the terminating number:

preg_match('/[0-9]+.{0,25}[^0-9]*[0-9]+\s/',$string,$matches);
return $matches[0];

There is no need to use str_word_count. If you insist on using it, say so in the comments and we can try to accommodate a solution using your thought process. However, preg_match is likely the most efficient way of accomplishing the whole task.

Upvotes: 1

Related Questions