Find the position of the first occurrence of any number in string

20,130

Solution 1

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?

Solution 2

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);
}

Solution 3

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

Share:
20,130
Gasper
Author by

Gasper

Updated on July 09, 2022

Comments

  • Gasper
    Gasper almost 2 years

    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');
    
  • Tim Pietzcker
    Tim Pietzcker over 12 years
    Makes more sense. I (and RegexBuddy) didn't know about PREG_OFFSET_CAPTURE :)
  • Gasper
    Gasper over 12 years
    if there is no number in string it returns length of original string?
  • Tim Pietzcker
    Tim Pietzcker over 12 years
    @gašper: Right. Can be fixed, but Stanislav's solution is much better.
  • Gasper
    Gasper over 12 years
    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).
  • Stanislav Shabalin
    Stanislav Shabalin over 12 years
    @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 :-)
  • Tim Pietzcker
    Tim Pietzcker over 12 years
    @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.
  • Roland Seuhs
    Roland Seuhs almost 5 years
    I bet that this is about 20 times faster than any regex solution
  • djdance
    djdance almost 2 years
    note: it works fine with non-latin text, but then don't use mb_substr