Credit Card Disguise and Bank Account Information

I am looking for a php function that can mask credit card and banking information such as route number and account numbers. I need to mask many formats, so existing answers don't help me much.

So, for example, if the input is 304-443-2456 , the function should return xxx-xxx-2456 . Sometimes a number has a dash and can be of different lengths.

I am looking for something in common that I can expand as necessary, preferably the auxiliary view class in the form of a zend framework.

+6
source share
4 answers

Some small regular expression in its function, available configuration:

 $number = '304-443-2456'; function mask_number($number, $count = 4, $seperators = '-') { $masked = preg_replace('/\d/', 'x', $number); $last = preg_match(sprintf('/([%s]?\d){%d}$/', preg_quote($seperators), $count), $number, $matches); if ($last) { list($clean) = $matches; $masked = substr($masked, 0, -strlen($clean)) . $clean; } return $masked; } echo mask_number($number); # xxx-xxx-2456 

If the function failed, it will return all masked ones (for example, another separator, less than 4 digits, etc.). In some cases, you can say about the safety of children.

Demo

+1
source
 <?php function ccmask($cc, $char = '#') { $pattern = '/^([0-9-]+)([0-9]*)$/U'; $matches = array(); preg_match($pattern, $cc, $matches); return preg_replace('([0-9])', $char, $matches[1]).$matches[2]; } echo ccmask('304-443-2456'), "\n"; echo ccmask('4924-7921-9900-9876', '*'), "\n"; echo ccmask('30-43-56', 'x'), "\n"; 

Outputs:

 ###-###-2456 ****-****-****-9876 xx-xx-56 
+1
source

I use a view helper for this. I tend to avoid Regex, although I always need years to understand what it does, especially if I return to the code after a while.

 class Zend_View_Helper_Ccmask { public function ccmask($ccNum) { $maskArray = explode('-', $ccNum); $sections = count($maskArray) - 1; for($i = 0; $i < $sections ; $i++){ $maskArray[$i] = str_replace(array(1,2,3,4,5,6,7,8,9,0), 'x', $maskArray[$i]); } return implode('-', $maskArray); } } 

In your opinion

 echo $this->ccmask('304-443-2456'); //output = xxx-xxx-2456 
+1
source

A good way to look at this is that it will not be masked and output xs for everything else. A simple, inexpensive solution:

 function cc_mask( $cc_raw, $unmask_count ){ $cc_masked = ''; for( $i=0; $i < ( strlen( $cc_raw ) - $unmask_count ) ; $i++ ){ //If you want to maintain hyphens and special characters $char = substr( $cc_raw, $i, 1 ); $cc_masked .= ctype_digit( $char ) ? "*" : $char; } $cc_masked .= substr( $cc_raw , -$unmask_count ); return $cc_masked; } echo cc_mask("304-443-2456",4); //Output ***-***-2456 

It would be even faster if there was no need to support hyphens and special characters

0
source

Source: https://habr.com/ru/post/899000/


All Articles