duanjunjie0196
2016-08-08 10:10
浏览 295
已采纳

从变量中提取第一个数字

I have variables that always start with a number or numbers and need the script to determine where this number ends, now for an example the variable can be like the following:

1234-hello1.jpg

1 hello1.gif

1234hello1.gif

123456 hello1.gif

What I am trying to say is an explode function would not work, and my regex is very poor, I just need to be left with the first number and ignore any other number in the string. I just need to be left with the number(s) in bold.

Thanks in advance...

图片转代码服务由CSDN问答提供 功能建议

我的变量总是以一个或多个数字开头,需要脚本来确定这个数字的结束位置,现在是 例如,变量可以如下所示:

1234 -hello1.jpg

1 hello1.gif

1234 hello1.gif

123456 hello1.gif < / p>

我想说的是爆炸功能不起作用,我的正则表达式很差,我只需要留下第一个数字并忽略字符串中的任何其他数字。 我只需要留下粗体数字。

提前致谢...

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • dpg78570 2016-08-08 10:19
    已采纳
    $arr = str_split($str);
    for($i = 0; $i < count($arr); ++$i){
       if(!is_numeric($arr[$i])){
           echo "Number ends at index: " . $i;
           break;
       }
    }
    

    You could also put the numbers into an array using $arr[$i] if you so wish. This is probably a lot more readable than using regex.

    You could add logic to allow one decimal point but from the question it seems you only want integers.

    http://sandbox.onlinephpfunctions.com/code/fd21437e8c1502b56572a624cf6e4683cf483a8d - Example of working code

    点赞 评论
  • dongtan7418 2016-08-08 10:15

    I think you can remove no from your sting with below code.

    preg_replace('/[0-9]+/', '', $string);
    

    here $string is variable you can change this name as per your variable name.

    点赞 评论
  • drwo2014 2016-08-08 10:16

    A RegEx is indeed the way to go:

    function getVal($var){
        $retVal = '';
    
        if(preg_match('#^(\d+)#', $var, $aCapture)){  //Look for the digits
            $retVal = $aCapture[1];    //Use the digits captured in the brackets from the RegEx match
        }
        return $retVal;
    }
    

    What this does is look for only digits at the start of the string, capture them in an array, and use the piece we want.

    点赞 评论
  • drmticpet66231422 2016-08-08 10:17

    preg_match('/^[0-9]+/', $yourString, $match);

    Now you can check $match for the integer.

    点赞 评论
  • duangou6446 2016-08-08 10:21

    If you are sure that the number is an integer, at the beginning and always present, you can use sscanf:

    echo sscanf($val, '%d')[0];
    
    点赞 评论
  • douludi8413 2016-08-08 10:35

    This is the RegEx what you need:

    ^.*\b([0-9]+)
    

    I don't know which language you are writting, so just give you the RegEx. It's tested in Notepad++ with your Examples.

    点赞 评论
  • dongyirong3564 2016-08-08 11:11

    Peter Bennett, You can try like this. First, convert a string(1234-hello1.jpg) to an Array. then you can check whether given array element is Number or not.

    $str = "1234-hello1.jpg";       //given string
    $count = strlen($str);          //count length of string
    $num = array();
    
    for($i=0; $i < $count; $i++)
    {
        if(is_numeric($str[$i]))     //to check element is Number or Not
        {
            $num[] = $str[$i];       //if it's number, than add it to another array
        }
        else break;                  //if array element is not a number. exit **For** loop
    }
    
    $number = $num;                //See o/p
    $number = implode("", $number);   
    echo $number;                    // Now $number is String.
    

    Out Put

     $num = Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
    );
    
    
    $number = "1234";   //string
    

    So finally you got your required string.

    点赞 评论
  • dongzouqie4220 2016-08-08 11:47

    Here is the full working script, with thanks to @user1...

    $str = "1234-hello1.jpg";
    $arr = str_split($str);
    for($i = 0; $i < count($arr); ++$i){
       if(!is_numeric($arr[$i])){
           //echo "Number ends at index: " . $i;       
           break;
      } else {
            $num[] = $str[$i];   
       }
    }
    
    $fullNumber = join("", $num);
    
    echo $fullNumber;
    
    点赞 评论

相关推荐 更多相似问题