12

Can someone help me with algorithm for finding the position of the first occurrence of any number in a string?

The code I found on the web does not work:

function my_offset($text){
    preg_match('/^[^\-]*-\D*/', $text, $m);
    return strlen($m[0]);
}
echo my_offset('[HorribleSubs] Bleach - 311 [720p].mkv');
ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Gasper
  • 959
  • 3
  • 11
  • 29

5 Answers5

24

The built-in PHP function strcspn() will do the same as the function in Stanislav Shabalin's answer when used like so:

strcspn( $str , '0123456789' )

Examples:

echo strcspn( 'That will be $2.95 with a coupon.' , '0123456789' ); // 14
echo strcspn( '12 people said yes'                , '0123456789' ); // 0
echo strcspn( 'You are number one!'               , '0123456789' ); // 19

HTH

zavaboy
  • 341
  • 2
  • 4
20
function my_offset($text) {
    preg_match('/\d/', $text, $m, PREG_OFFSET_CAPTURE);
    if (sizeof($m))
        return $m[0][1]; // 24 in your example

    // return anything you need for the case when there's no numbers in the string
    return strlen($text);
}
Stanislav Shabalin
  • 19,028
  • 3
  • 18
  • 18
15
function my_ofset($text){
    preg_match('/^\D*(?=\d)/', $text, $m);
    return isset($m[0]) ? strlen($m[0]) : false;
}

should work for this. The original code required a - to come before the first number, perhaps that was the problem?

mircobabini
  • 544
  • 2
  • 13
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • if there is no number in string it returns length of original string? – Gasper Sep 21 '11 at 07:00
  • @gašper: Right. Can be fixed, but Stanislav's solution is much better. – Tim Pietzcker Sep 21 '11 at 07:03
  • Why is his solution better? if there is no number in string it returns me 1 (with his algorithm). if i use yours it returns me something logical (string length). – Gasper Sep 21 '11 at 07:10
  • @gašper I've edited mine. If there's no number it now returns string length as well. I guess it's better because a) it searches exactly for numbers, not for non-numbers and b) it utilizes `preg_match` built-in feature which is always better than reinventing a bike. I hope Tim agrees with me :-) – Stanislav Shabalin Sep 21 '11 at 07:27
  • @gašper: Well, my solution is kind of bent-over backwards (count the number of non-digits until the first digit). Also, thinking about it, I expect my solution to require an additional check whether there has been a match at all. I'm not a PHP programmer, so I'm building that code upon snippets created by RegexBuddy using my regex. – Tim Pietzcker Sep 21 '11 at 07:56
0

I can do regular expressions but I have to go into an altered state to remember what it does after I've coded it.

Here is a simple PHP function you can use...

function findFirstNum($myString) {

    $slength = strlen($myString);

    for ($index = 0;  $index < $slength; $index++)
    {
        $char = substr($myString, $index, 1);

        if (is_numeric($char))
        {
            return $index;
        }
    }

    return 0;  //no numbers found
}
YakovL
  • 7,557
  • 12
  • 62
  • 102
ProfPerry
  • 11
  • 2
0

Problem

Find the first occurring number in a string

Solution

Here is a non regex solution in javascript

var findFirstNum = function(str) {
    let i = 0;
    let result = "";
    let value;
    while (i<str.length) {
      if(!isNaN(parseInt(str[i]))) {
        if (str[i-1] === "-") {
          result = "-";
        }
        while (!isNaN(parseInt(str[i])) && i<str.length) {
          result = result + str[i];
          i++;
        }
        break;
      }
      i++;
    }
    return parseInt(result);  
};

Example Input

findFirstNum("words and -987 555");

Output

-987