PHP amount verification function

During the development process, more or less the amount will be worth checking. The tester slapped my ass and asked me this question every time, which was not good for me. Simple regularization can no longer exclude the values ​​of -0,0,0.00,01,0001. Therefore, write a general function to determine whether it is the correct amount format:

/**            
 * 金额校验函数
 * @param $value
 * @param bool $isZero
 * @param bool $negative
 * @return bool
 */
function isAmount($value, $isZero=false, $negative=false){
    // 必须是整数或浮点数,且允许为负
    if (!preg_match("/^[-]?\d+(.\d{1,2})?$/", $value)){
        return false;
    }
    // 不为 0
    if (!$isZero && empty((int)($value*100))){
        return false;
    }
    // 不为负数
    if (!$negative && (int)($value * 100) < 0){
        return false;
    }
    return true;
}

 

Guess you like

Origin blog.csdn.net/z3287852/article/details/112791717