掩盖信用卡和银行账户信息

发布于 2024-12-09 09:25:00 字数 230 浏览 0 评论 0原文

我正在寻找一个可以屏蔽信用卡和信用卡的 php 函数。银行信息,例如路由号码和帐号。我需要屏蔽多种格式,因此现有的堆栈溢出答案对我帮助不大。

例如,如果输入为 304-443-2456,则该函数应返回 xxx-xxx-2456。 有时该数字带有破折号,并且可以有不同的长度。

我正在寻找一些通用的东西,我可以根据需要扩展它,最好是 zend 框架视图帮助程序类。

I'm looking for a php function which can mask credit card & bank information such as routing number and account numbers. I need to mask many formats, so the existing stack overflow answers don't help me that much.

So for example, if the input is 304-443-2456, the function should return xxx-xxx-2456.
Sometimes the number has dashes, and can be in various lengths.

I'm looking for something generic, that I can extend as needed, preferably a zend framework view helper class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失而复得 2024-12-16 09:25:00

一些小的正则表达式在它自己的函数中,可用配置:

$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

如果函数失败,它将返回所有屏蔽的(例如不同的分隔符,少于4位数字等)。您可以说内置了一些儿童安全措施。

演示

Some little regex in a function of it own, configuration available:

$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 fails, it will return all masked (e.g. a different seperator, less than 4 digits etc.). Some child-safety build in you could say.

Demo

天赋异禀 2024-12-16 09:25:00
<?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";  

输出:

###-###-2456
****-****-****-9876
xx-xx-56
<?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
誰認得朕 2024-12-16 09:25:00

我为此使用了一个视图助手。我倾向于避免使用正则表达式,因为我总是需要很长时间才能弄清楚它的作用,特别是当我在一段时间后回到代码时。

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);
    }
}

在你看来

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

I use a view helper for that. I tend to avoid Regex though as it always takes me ages to work out what it does, especially if I come back to 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 view

echo $this->ccmask('304-443-2456');
//output = xxx-xxx-2456
ペ泪落弦音 2024-12-16 09:25:00

查看此问题的一个好方法是查看哪些内容不会被屏蔽,并为其他所有内容输出 xs。一个简单、廉价的解决方案是:

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

如果不需要维护连字符和特殊字符,速度会更快

A good way to look at this is at what won't be masked and outputing xs for everything else. A simple, inexpensive solution is:

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

Would be even faster if there was no need to maintain the hyphens and special characters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文