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

从变量中提取第一个数字

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...

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集