dongpei2835 2019-02-19 15:31
浏览 101
已采纳

找到字符串中大于特定数字的任何数字的第一个出现位置

I have this string: "20 plus 1000 is not 20000."

And I want to get the position of the first number bigger than 10000.

I'm actualy (and for a different purpose) using this first answer code to get the position of the first number in my string:

function firstNumPos($text){
  preg_match('/^\D*(?=\d)/', $text, $m);
  return isset($m[0]) ? strlen($m[0]) : false;
}

But it doesn't distinguish between number sizes, so, what could be missing for this function to get the position of the first occurence of a number bigger than 10000?

Any solution is welcome, not necessarily by using that function.

  • 写回答

5条回答 默认 最新

  • dongwei9771 2019-02-19 15:44
    关注
    function firstNumPos($text, $number){
      preg_match_all('!\d+!', $text, $match);
      foreach ($match[0] as $value) {
          if ($value > $number) {
              return strpos($text, $value);
          }
      }
    }
    
    echo firstNumPos('20 plus 1000 is not 20000.', 10000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题