check serial match

this code is written is by myself,to check serial is match,thanks

<?php
/*
 * check serial match,like give two serial:ab-cde-000000123,ab-cde-000000234
 * copy right http://zhimingji.com/
 */
$test	=	test::checkSerialMatch('ab-cde-000000123','ab-cde-000000124');
var_dump($test);
$test	=	test::checkSerialMatch('ab-cde-000000123','ab-cde-000000122');
var_dump($test);
$test	=	test::checkSerialMatch('ab-cde-000000123','ab-cde-000000123');
var_dump($test);
class test
{
    static public function checkSerialMatch($startSerial,$endSerial)
    {
        $result                    =    false;
        $checkStartSerialRight    =    self::checkSerialFormat($startSerial);
        $checkendSerialRight    =    self::checkSerialFormat($endSerial);
        if($checkStartSerialRight==true&&$checkendSerialRight==true)
        {
            //echo  '30';
            $startSerialPrefix    =    self::getSerialPrefix($startSerial);
            $endSerialPrefix    =    self::getSerialPrefix($endSerial);
            if($startSerialPrefix==$endSerialPrefix)
            {
                $startSerial    =    preg_replace('/^'.$startSerialPrefix.'/','',$startSerial);
                $endSerial    =    preg_replace('/^'.$endSerialPrefix.'/','',$endSerial);
                $startSerialNum    =    preg_replace('/^0*/','',$startSerial);
                $endSerialNum    =    preg_replace('/^0*/','',$endSerial);
                if($startSerialNum<=$endSerialNum)
                {
                    $result    =    true;
                }
            }
        }
        return $result;
    }
    static public function checkSerialFormat($serial)
    {
        if(strlen($serial)!=16)
        {
            //echo  '310';
            return false;
        }
//        $serialPrefix    =    substr($serial,0,7);
//        $serialPrefixArr=    explode('|', SERIAL_PREFIX);
//        if(!in_array($serialPrefix, $serialPrefixArr))
//        {
//            //echo  '311';
//            return false;
//        }
        $serialNum        =    substr($serial,-9);
        if(!is_numeric($serialNum))
        {
            //echo  '312';
            return false;
        }
        return true;
    }
    static public function getSerialPrefix($serial)
    {
        if(strlen($serial)!=16)
        {
            return false;
        }
        $serialPrefix    =    substr($serial,0,7);
        return $serialPrefix;       
    }
    static public function getSerialSuffix($serial,$removeZeroFlag=true)
    {
        $return    =    false;
        if(strlen($serial)==16)
        {
            $serialSuffix    =    substr($serial,-9);
            if($removeZeroFlag=true)
            {
                $serialSuffix    =    preg_replace('/^0*/', '', $serialSuffix);
            }
            $return            =    $serialSuffix;
        }
        return $return;       
    }
}

Related posts:

Leave a Reply

Your email address will not be published.