【问题标题】:Extract first number(s) from variable从变量中提取第一个数字
【发布时间】:2016-08-08 10:10:51
【问题描述】:

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

1234-hello1.jpg

1 hello1.gif

1234hello1.gif

123456 hello1.gif

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

提前谢谢...

【问题讨论】:

  • 数字总是整数吗?号码是否始终存在?数字总是在开头吗?
  • 我没有得到你所期望的结果。请为每个给定的字符串显示您想要的结果。

标签: php preg-match explode strpos


【解决方案1】:
$arr = str_split($str);
for($i = 0; $i < count($arr); ++$i){
   if(!is_numeric($arr[$i])){
       echo "Number ends at index: " . $i;
       break;
   }
}

如果您愿意,也可以使用 $arr[$i] 将数字放入数组中。 这可能比使用正则表达式更具可读性。

您可以添加逻辑以允许小数点后一位,但从问题看来您只需要整数。

http://sandbox.onlinephpfunctions.com/code/fd21437e8c1502b56572a624cf6e4683cf483a8d - 工作代码示例

【讨论】:

  • 完美,只需要进行一些小的改动即可将整数放入单个变量中。 $str = "1234-hello1.jpg"; $arr = str_split($str); for($i = 0; $i &lt; count($arr); ++$i){ if(!is_numeric($arr[$i])){ //echo "Number ends at index: " . $i; break; } else { $num[] = $str[$i]; } } $fullNumber = join("", $num); echo $fullNumber;
【解决方案2】:

如果你确定数字是一个整数,在开头并且一直存在,你可以使用sscanf

echo sscanf($val, '%d')[0];

【讨论】:

    【解决方案3】:

    彼得·贝内特, 你可以这样试试。首先,将字符串(1234-hello1.jpg) 转换为数组。 然后你可以检查给定的数组元素是否是数字。

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

    输出

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

    所以最后你得到了你需要的字符串。

    【讨论】:

    • 您应该将其更改为公认的答案 - 这在每个 PHP 程序员最喜欢的函数工具包中都非常有效。
    【解决方案4】:

    我认为您可以使用以下代码从您的刺痛中删除 no。

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

    这里$string 是变量,您可以根据变量名称更改此名称。

    【讨论】:

    • 这与 OP 想要的相反。他只想要第一个数字。这将返回除第一个数字之外的所有内容。
    【解决方案5】:

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

    现在您可以检查$match 的整数。

    【讨论】:

      【解决方案6】:

      RegEx 确实是要走的路:

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

      它的作用是只查找字符串开头的数字,将它们捕获到一个数组中,然后使用我们想要的部分。

      【讨论】:

        【解决方案7】:

        这是您需要的正则表达式:

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

        我不知道你在写哪种语言,所以只要给你 RegEx。它已在 Notepad++ 中使用您的示例进行了测试。

        【讨论】:

          【解决方案8】:

          这是完整的工作脚本,感谢@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;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-02
            • 2013-02-24
            • 1970-01-01
            • 2014-11-11
            相关资源
            最近更新 更多