LoginSignup
2

More than 5 years have passed since last update.

PHPである変数がDBのINTEGERやBIGINTの範囲にあるかテストする正規表現

Last updated at Posted at 2016-10-25

動機

PHPではPHP_INT_MAX(2147483647か9223372036854775807)の値より大きい数値が上手に扱えないので正規表現で頑張ってみました。
ざっくりしたテストしかしてないのでおかしい所があったら教えてください:droplet:

ISクラス

IS.php
<?php
class IS {
    /**
     * INT UNSIGNEDの範囲内か?
     *     987654321
     * 0~4294967295
     */
    static $INT_UNSIGNED;
    public static function INT_UNSIGNED($_){
        if( !static::$INT_UNSIGNED ) static::$INT_UNSIGNED = '/^(' . implode('|',[
            '0'                     ,// 0987654321
            '[1-9]\d{0,8}'          ,//  *DDDDDDDD
            '[1-3]\d{9}'            ,// *DDDDDDDDD
            '4[0-1]\d{8}'           ,// 4*DDDDDDDD
            '42[0-8]\d{7}'          ,// 42*DDDDDDD
            '429[0-3]\d{6}'         ,// 429*DDDDDD
            '4294[0-8]\d{5}'        ,// 4294*DDDDD
            '42949[0-5]\d{4}'       ,// 42949*DDDD
            '429496[0-6]\d{3}'      ,// 429496*DDD
            '4294967[0-1]\d{2}'     ,// 4294967*DD
            '42949672[0-8][0-9]'    ,// 42949672*D
            '429496729[0-5]'        ,// 4294967295
        ]) . ')$/';
        return preg_match(static::$INT_UNSIGNED,$_);
    }
    /**
     * INT SIGNEDの範囲内か?
     *   987654321   987654321
     * -2147483648~2147483647
     */
    static $INT_SIGNED;
    public static function INT_SIGNED($_){
        if( !static::$INT_SIGNED ) static::$INT_SIGNED = '/^(' . implode('|',[
            '0'                     ,//  0987654321
            '-?[1-9]\d{0,8}'        ,// - *DDDDDDDD
            '-?1\d{9}'              ,// -1DDDDDDDDD
            '-?20\d{8}'             ,// -20DDDDDDDD
            '-?21[0-3]\d{7}'        ,// -21*DDDDDDD
            '-?214[0-6]\d{6}'       ,// -214*DDDDDD
            '-?2147[0-3]\d{5}'      ,// -2147*DDDDD
            '-?21474[0-7]\d{4}'     ,// -21474*DDDD
            '-?214748[0-2]\d{3}'    ,// -214748*DDD
            '-?2147483[0-5]\d{2}'   ,// -2147483*DD
            '-?21474836[0-3][0-9]'  ,// -21474836*D
            '-214748364[0-8]'       ,// -2147483648
            '214748364[0-7]'        ,//  2147483647
        ]) . ')$/';
        return preg_match(static::$INT_SIGNED,$_,$match);
    }
    /**
     * BIGINT UNSIGNEDの範囲内か?
          09876543210987654321
     * 0~18446744073709551615
     */
    static $BIGINT_UNSIGNED;
    public static function BIGINT_UNSIGNED($_){
        if( !static::$BIGINT_UNSIGNED ) static::$BIGINT_UNSIGNED = '/^(' . implode('|',[
            '0'                             ,// 09876543210987654321
            '[1-9]\d{0,18}'                 ,//  *DDDDDDDDDDDDDDDDDD
            '1[0-7]\d{18}'                  ,// 1*DDDDDDDDDDDDDDDDDD
            '18[0-3]\d{17}'                 ,// 18*DDDDDDDDDDDDDDDDD
            '184[0-3]\d{16}'                ,// 184*DDDDDDDDDDDDDDDD
            '1844[0-5]\d{15}'               ,// 1844*DDDDDDDDDDDDDDD
            '18446[0-6]\d{14}'              ,// 18446*DDDDDDDDDDDDDD
            '184467[0-3]\d{13}'             ,// 184467*DDDDDDDDDDDDD
            '1844674[0-3]\d{12}'            ,// 1844674*DDDDDDDDDDDD
            '184467440[0-6]\d{10}'          ,// 184467440*DDDDDDDDDD
            '1844674407[0-2]\d{9}'          ,// 1844674407*DDDDDDDDD
            '18446744073[0-6]\d{8}'         ,// 18446744073*DDDDDDDD
            '1844674407370[0-8]\d{6}'       ,// 1844674407370*DDDDDD
            '18446744073709[0-4]\d{5}'      ,// 18446744073709*DDDDD
            '184467440737095[0-4]\d{4}'     ,// 184467440737095*DDDD
            '18446744073709550\d{3}'        ,// 18446744073709550DDD
            '18446744073709551[0-5]\d{2}'   ,// 18446744073709551*DD
            '1844674407370955160[0-9]'      ,// 1844674407370955160D
            '1844674407370955161[0-5]'      ,// 18446744073709551615
        ]) . ')$/';
        return preg_match(static::$BIGINT_UNSIGNED,$_);
    }
    /**
     * BIGINT SIGNEDの範囲内か?
     *  9876543210987654321  9876543210987654321
     * -9223372036854775808~9223372036854775807
     */
    static $BIGINT_SIGNED;
    public static function BIGINT_SIGNED($_){
        if( !static::$BIGINT_SIGNED ) static::$BIGINT_SIGNED = '/^(' . implode('|',[
            '0'                             ,//  9876543210987654321
            '-?[1-9]\d{0,17}'               ,// - *DDDDDDDDDDDDDDDDD
            '-?[1-8]\d{18}'                 ,// -*DDDDDDDDDDDDDDDDDD
            '-?9[0-1]\d{17}'                ,// -9*DDDDDDDDDDDDDDDDD
            '-?92[0-1]\d{16}'               ,// -92*DDDDDDDDDDDDDDDD
            '-?922[0-2]\d{15}'              ,// -922*DDDDDDDDDDDDDDD
            '-?9223[0-2]\d{14}'             ,// -9223*DDDDDDDDDDDDDD
            '-?92233[0-6]\d{13}'            ,// -92233*DDDDDDDDDDDDD
            '-?922337[0-1]\d{12}'           ,// -922337*DDDDDDDDDDDD
            '-?92233720[0-2]\d{10}'         ,// -92233720*DDDDDDDDDD
            '-?922337203[0-5]\d{9}'         ,// -922337203*DDDDDDDDD
            '-?9223372036[0-7]\d{8}'        ,// -9223372036*DDDDDDDD
            '-?92233720368[0-4]\d{7}'       ,// -92233720368*DDDDDDD
            '-?922337203685[0-3]\d{6}'      ,// -922337203685*DDDDDD
            '-?9223372036854[0-6]\d{5}'     ,// -9223372036854*DDDDD
            '-?92233720368547[0-6]\d{4}'    ,// -92233720368547*DDDD
            '-?922337203685477[0-4]\d{3}'   ,// -922337203685477*DDD
            '-?9223372036854775[0-7]\d{2}'  ,// -9223372036854775*DD
            '-922337203685477580[0-8]'      ,// -9223372036854775808
            '922337203685477580[0-7]'       ,//  9223372036854775807
        ]) . ')$/';
        return preg_match(static::$BIGINT_SIGNED,$_);
    }
}

テストコード

test.php
<?php
    if( !IS::INT_UNSIGNED(-2147483649) ) echo -2147483649,"\n";
    if( !IS::INT_UNSIGNED(-2147483648) ) echo -2147483648,"\n";
    if( !IS::INT_UNSIGNED(-2147483647) ) echo -2147483647,"\n";
    if( !IS::INT_UNSIGNED(0) ) echo 0,"\n";
    if( !IS::INT_UNSIGNED(2147483646) ) echo 2147483646,"\n";
    if( !IS::INT_UNSIGNED(2147483647) ) echo 2147483647,"\n";
    if( !IS::INT_UNSIGNED(2147483648) ) echo 2147483648,"\n";
    if( !IS::INT_UNSIGNED(2147483649) ) echo 2147483649,"\n";

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2