Home  >  Article  >  Backend Development  >  How to use PHP regular expression to verify whether the input string is in the correct bank card number format

How to use PHP regular expression to verify whether the input string is in the correct bank card number format

PHPz
PHPzOriginal
2023-06-24 10:02:511410browse

With the rapid development of modern economy, financial transactions are becoming increasingly frequent. As a common payment method, bank cards must ensure their safety and accuracy. Therefore, it is very important to verify the correctness of the bank card number when conducting bank card transactions. This article will introduce how to use PHP regular expressions to verify whether the input string is in the correct bank card number format.

Bank card numbers are usually verified using the Luhn algorithm, that is, each digit of the card number is weighted one by one, the results are added, and finally the modulus is taken. If the result is 0, it means the bank card number format is correct. The specific operation is as follows:

1. Count from right to left, the last digit of the card number is the first, and then to the left, multiply each number by 2, if the number multiplied by 2 is greater than or equal to 10 , then split it into two numbers and add the two numbers.

2. Add all the numbers multiplied by 2.

3. Counting from right to left, the last digit of the card number is the first, and going to the left, multiply each number by 1 or not, and add the results.

4. Add the results of steps 2 and 3. If the result is divisible by 10, the bank card number format is correct.

The above verification process can be easily achieved using PHP regular expressions. The specific implementation steps are as follows:

Step 1: Format bank card number
Bank card number can only contain numbers, so you need to use PHP's preg_replace function to replace non-numeric characters in the string with spaces.

$bank_card_number = preg_replace('/D/','',$bank_card_number);

Step 2: Verify string length
The bank card number length is usually 16 or 19 digits , so you need to use PHP's strlen function to verify whether the string length is 16 or 19.

if(strlen($bank_card_number) !== 16 && strlen($bank_card_number) !== 19){

// 长度不正确

}

Step 3: Implement Luhn algorithm verification
Use PHP's preg_match function combined with regular expressions to implement Luhn algorithm verification.

$len = strlen($bank_card_number);
$sum = 0;
for($i = 0; $i < $len; $i ){

$value = intval($bank_card_number[$len - $i - 1]);
$sum += (($i % 2) !== 0) ? (($value * 2) >= 10 ? ($value * 2 - 9) : ($value * 2)) : $value;

}
if(($sum % 10) !== 0){

// 格式不正确

}

Now, we integrate the above code into a function to verify the bank card number format function.

function verify_bank_card_number($bank_card_number) {

$bank_card_number = preg_replace('/D/','',$bank_card_number);
if(strlen($bank_card_number) !== 16 && strlen($bank_card_number) !== 19){
    return false;
}
$len = strlen($bank_card_number);
$sum = 0;
for($i = 0; $i < $len; $i++){
    $value = intval($bank_card_number[$len - $i - 1]);
    $sum += (($i % 2) !== 0) ? (($value * 2) >= 10 ? ($value * 2 - 9) : ($value * 2)) : $value;
}
if(($sum % 10) !== 0){
    return false;
}
return true;

}

Call this function and pass in the bank card number string to determine whether the bank card number format is correct.

$bank_card_number = '6228480402564890018';
if(verify_bank_card_number($bank_card_number)){

echo '银行卡号格式正确';

} else {

echo '银行卡号格式不正确';

}

Passed With the above steps, we can use the preg_match function of PHP regular expressions to quickly verify whether the bank card number entered by the user is correct. This can effectively avoid problems such as transaction failure caused by incorrect input of bank card numbers.

The above is the detailed content of How to use PHP regular expression to verify whether the input string is in the correct bank card number format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn