1

I have this string: "20 plus 1000 is not 20000."

And I want to get the position of the first number bigger than 10000.

I'm actualy (and for a different purpose) using this first answer code to get the position of the first number in my string:

function firstNumPos($text){
  preg_match('/^\D*(?=\d)/', $text, $m);
  return isset($m[0]) ? strlen($m[0]) : false;
}

But it doesn't distinguish between number sizes, so, what could be missing for this function to get the position of the first occurence of a number bigger than 10000?

Any solution is welcome, not necessarily by using that function.

Gregor Albert
  • 819
  • 1
  • 11
  • 23

5 Answers5

1

You can get all the numbers with regex and then loop them and find the first bigger than 10000.

function getNumberPosition($text, $min = 10000){
    preg_match_all('!\d+!', $text, $matches);

    foreach ($matches[0] as $match) {
        if ($match >= $min) {
            return strpos($text, $match);
        }
    }
}

echo getNumber('20 plus 1000 is not 20000.');
Filip Š
  • 746
  • 2
  • 13
  • 22
1
function firstNumPos($text, $number){
  preg_match_all('!\d+!', $text, $match);
  foreach ($match[0] as $value) {
      if ($value > $number) {
          return strpos($text, $value);
      }
  }
}

echo firstNumPos('20 plus 1000 is not 20000.', 10000);
V-K
  • 1,297
  • 10
  • 28
1

If I don't misunderstood your question then you can try this way without using any fancy regex, Let's do it simple php way,

<?php
function findFirstOccurance($str){
    $arr = explode(' ',$str);
    $counter = 0;
    foreach($arr as $value){
        $counter=$counter+strlen($value);
        if($value > 10000){
            return $counter - 1;
        }
    }
}
$str = "20 plus 1000 is not 20000.";
echo findFirstOccurance($str);
?>

DEMO: https://3v4l.org/NNDZk

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
1

Simply use this regex

\d\d\d\d\d\d*

to pull out matches on all numbers with 5 digits or more. Then you can simply check for the first match and return it

function getFirstOccr($srcStr, $places=5)
{
//    $matchStr = '\d{'.$places.'}\d*';
    $matchStr = str_repeat('\d', $places).'\d*';
    preg_match('/'.$matchStr.'/', $srcStr, $matches);

    return isset($matches[0]) ? strpos($srcStr,$matches[0]) : false;
}

$srcStr = "20 plus 1000 is not 20000.";
print getFirstOccr($srcStr,5);

I believe this should be most computationally efficient

Jc Nolan
  • 450
  • 4
  • 15
  • Ah, sorry I was coding off the top of my head and have been exclusively working in Swift the past 3 years. It is fixed and checked now and added the option for the alternate regex someone else recommend. This code with that regex should be the most efficient I believe – Jc Nolan Feb 19 '19 at 17:00
0

If you are always going to use 10000, it may be easy enough to look for the first 5 digit number using \d{5} and if you want the character position, then use PREG_OFFSET_CAPTURE...

function firstNumPos($text){
    preg_match('/(\d{5})/', $text, $m, PREG_OFFSET_CAPTURE);
    return isset($m[0]) ? $m[0][1] : false;
}

echo firstNumPos('20 plus 1000 is not 20000.');
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55