【问题标题】:Get part of digit in separate variables在单独的变量中获取部分数字
【发布时间】:2016-01-06 18:05:30
【问题描述】:

我有号码 14969126,想分开喜欢

$last_five_digits = 69126;
$rest_of_digits = 149;

我试过了

$last_five_digits = substr($postalcode, -5);

但是我怎样才能得到其余的数字。

请提供任何建议。

谢谢。

【问题讨论】:

    标签: php regex preg-match preg-match-all substr


    【解决方案1】:

    你可以使用:

    $firstDigits = substr($postalcode, 0, -5);
    //=> 149
    

    【讨论】:

      【解决方案2】:

      preg_split() 返回一个数组:

      list($first, $last_five) =
          preg_split('/(\d{5})$/', '14969126', null, PREG_SPLIT_DELIM_CAPTURE);
      

      【讨论】:

        【解决方案3】:
        $postalcode = 14969126;
        
         $last_five_digits = substr($postalcode, -5);
         $rest_of_digits = substr($postalcode,0,3);
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
        猜你喜欢
        • 2015-02-27
        • 1970-01-01
        • 2015-03-13
        • 2021-03-24
        • 1970-01-01
        • 2017-08-19
        • 2015-08-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多