當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Valid::luhn方法代碼示例

本文整理匯總了PHP中Valid::luhn方法的典型用法代碼示例。如果您正苦於以下問題:PHP Valid::luhn方法的具體用法?PHP Valid::luhn怎麽用?PHP Valid::luhn使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Valid的用法示例。


在下文中一共展示了Valid::luhn方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: credit_card

 /**
  * Validates a credit card number, with a Luhn check if possible.
  *
  * @param   integer         $number credit card number
  * @param   string|array    $type   card type, or an array of card types
  * @return  boolean
  * @uses    Valid::luhn
  */
 public static function credit_card($number, $type = NULL)
 {
     // Remove all non-digit characters from the number
     if (($number = preg_replace('/\\D+/', '', $number)) === '') {
         return FALSE;
     }
     if ($type == NULL) {
         // Use the default type
         $type = 'default';
     } elseif (is_array($type)) {
         foreach ($type as $t) {
             // Test each type for validity
             if (Valid::credit_card($number, $t)) {
                 return TRUE;
             }
         }
         return FALSE;
     }
     $cards = Kohana::config('credit_cards');
     // Check card type
     $type = strtolower($type);
     if (!isset($cards[$type])) {
         return FALSE;
     }
     // Check card number length
     $length = strlen($number);
     // Validate the card length by the card type
     if (!in_array($length, preg_split('/\\D+/', $cards[$type]['length']))) {
         return FALSE;
     }
     // Check card number prefix
     if (!preg_match('/^' . $cards[$type]['prefix'] . '/', $number)) {
         return FALSE;
     }
     // No Luhn check required
     if ($cards[$type]['luhn'] == FALSE) {
         return TRUE;
     }
     return Valid::luhn($number);
 }
開發者ID:andygoo,項目名稱:kohana,代碼行數:48,代碼來源:Valid.php

示例2: test_luhn

 /**
  * Tests Valid::luhn()
  *
  * @test
  * @covers Valid::luhn
  * @dataProvider  provider_luhn()
  * @param string  $number   Credit card number
  * @param boolean $expected
  */
 public function test_luhn($number, $expected)
 {
     $this->assertSame($expected, Valid::luhn($number));
 }
開發者ID:reznikds,項目名稱:Reznik,代碼行數:13,代碼來源:ValidTest.php

示例3: credit_card

 /**
  * Validates a credit card number, with a Luhn check if possible.
  *
  * @param   integer         $number credit card number
  * @param   string|array    $type   card type, or an array of card types
  * @return  boolean
  * @uses    Valid::luhn
  */
 public static function credit_card($number, $type = NULL)
 {
     // Remove all non-digit characters from the number
     if (($number = preg_replace('/\\D+/', '', $number)) === '') {
         return FALSE;
     }
     if ($type == NULL) {
         // Use the default type
         $type = 'default';
     } elseif (is_array($type)) {
         foreach ($type as $t) {
             // Test each type for validity
             if (Valid::credit_card($number, $t)) {
                 return TRUE;
             }
         }
         return FALSE;
     }
     $cards = array('default' => array('length' => '13,14,15,16,17,18,19', 'prefix' => '', 'luhn' => TRUE), 'american express' => array('length' => '15', 'prefix' => '3[47]', 'luhn' => TRUE), 'diners club' => array('length' => '14,16', 'prefix' => '36|55|30[0-5]', 'luhn' => TRUE), 'discover' => array('length' => '16', 'prefix' => '6(?:5|011)', 'luhn' => TRUE), 'jcb' => array('length' => '15,16', 'prefix' => '3|1800|2131', 'luhn' => TRUE), 'maestro' => array('length' => '16,18', 'prefix' => '50(?:20|38)|6(?:304|759)', 'luhn' => TRUE), 'mastercard' => array('length' => '16', 'prefix' => '5[1-5]', 'luhn' => TRUE), 'visa' => array('length' => '13,16', 'prefix' => '4', 'luhn' => TRUE));
     // Check card type
     $type = strtolower($type);
     if (!isset($cards[$type])) {
         return FALSE;
     }
     // Check card number length
     $length = strlen($number);
     // Validate the card length by the card type
     if (!in_array($length, preg_split('/\\D+/', $cards[$type]['length']))) {
         return FALSE;
     }
     // Check card number prefix
     if (!preg_match('/^' . $cards[$type]['prefix'] . '/', $number)) {
         return FALSE;
     }
     // No Luhn check required
     if ($cards[$type]['luhn'] == FALSE) {
         return TRUE;
     }
     return Valid::luhn($number);
 }
開發者ID:gnribeiro,項目名稱:turim,代碼行數:48,代碼來源:Valid.php


注:本文中的Valid::luhn方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。