PHP如何實現校驗信用卡卡號碼是否有問題?本文主要介紹了PHP實作透過Luhn演算法校驗信用卡卡號是否有效,透過實例分析了php實作Luhn演算法及相關應用技巧。希望對大家有幫助。
具體實作方法如下:
$numbers = "49927398716 49927398717 1234567812345678 1234567812345670"; foreach (split(' ', $numbers) as $n) echo "$n is ", luhnTest($n) ? 'valid' : 'not valid', '</br>'; function luhnTest($num) { $len = strlen($num); for ($i = $len-1; $i >= 0; $i--) { $ord = ord($num[$i]); if (($len - 1) & $i) { $sum += $ord; } else { $sum += $ord / 5 + (2 * $ord) % 10; } } return $sum % 10 == 0; }
#執行結果
##
49927398716 is valid 49927398717 is not valid 1234567812345678 is not valid 1234567812345670 is valid
function luhn_test($num) { $str = ''; foreach( array_reverse( str_split( $num ) ) as $i => $c ) $str .= ($i % 2 ? $c * 2 : $c ); return array_sum( str_split($str) ) % 10 == 0; } foreach (array('49927398716','49927398717','1234567812345678','1234567812345670') as $n) echo "$n is ", luhn_test($n) ? 'valid' : 'not valid', "</br>\n";
##
49927398716 is valid 49927398717 is not valid 1234567812345678 is not valid 1234567812345670 is valid
##php判斷訪客是否為搜尋引擎蜘蛛的函式代碼PHP 判斷客戶端是IOS還是Android
以上是PHP校驗信用卡號實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!