1 /** 2 * 验证银行卡号是否是信用卡 3 * @param $cardnumber 4 * @return bool 5 */ 6 function validateCard ($cardnumber) 7 { 8 $cardnumber = preg_replace ("/D|s/","", $cardnumber); 9 $cardlength = strlen($cardnumber); 10 if($cardlength !=0){ 11 $parity = $cardlength %2; 12 $sum =0; 13 for($i=0; $i<$cardlength; $i++){ 14 $digit = $cardnumber[$i]; 15 if($i %2== $parity) $digit = $digit *2; 16 if($digit >9) $digit = $digit -9; 17 $sum = $sum + $digit; 18 } 19 $valid =($sum %10==0); 20 return $valid; 21 } 22 return false; 23 }