Get first number in a string after the first occurrence of identifier

前端 未结 3 525
猫巷女王i
猫巷女王i 2021-01-17 04:19

I\'m working on a function that gets a string like this:

identifier 20 j. - cat: text text text aaaa ffffdd ..... cccc 60\' - text, 2008

and

3条回答
  •  余生分开走
    2021-01-17 04:53

    You can use a regular expression for this:

    $matches = array();
    preg_match('/identifier\s*(\d+)/', $string, $matches);
    var_dump($matches);
    

    \s* is whitespace. (\d+) matches a number.

    You can wrap it in a function:

    function matchIdentifier($string) {
        $matches = array();
        if (!preg_match('/identifier\s*(\d+)/', $string, $matches)) {
            return null;
        }
        return $matches[1];
    }
    

提交回复
热议问题