Last active
October 3, 2017 10:01
-
-
Save sergeliatko/3f4d8cb27e4e91c6c1086c654008b5c4 to your computer and use it in GitHub Desktop.
PHP funtion to mask credit card number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Masks credit card number leaving up to 4 last available digits. | |
* | |
* @param string $n Credit card number (up to 16 characters) | |
* @param string $m Masking string | |
* @param int $v Maximum number of visible characters at the end of the credit card number (in range of 0-4) | |
* | |
* @return string | |
*/ | |
function vendor_mask_card_number( $n = '', $m = 'X', $v = 4 ) { | |
$n = substr( $n, -16 ); | |
$v = ( 4 < ( $v = abs( intval( $v ) ) ) ) ? 4 : $v; | |
$l = ( ( 16 - $v ) > ( $l = abs( intval( 16 - strlen( $n ) ) ) ) ) ? 12 : $l; | |
return substr_replace( str_pad( $n, 16, $m, STR_PAD_LEFT ), str_repeat( $m, $l ), 0, $l ); | |
} |
Author
sergeliatko
commented
Oct 3, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment