我有信用卡号,我想屏蔽如下:
$cc = 1234123412341234
echo cc_masking($cc)
1234XXXXXXXX1234
function cc_masking($number) {.....}
请为此建议正则表达式。
我有信用卡号,我想屏蔽如下:
$cc = 1234123412341234
echo cc_masking($cc)
1234XXXXXXXX1234
function cc_masking($number) {.....}
请为此建议正则表达式。
这应该使用substr
:
function ccMasking($number, $maskingCharacter = 'X') {
return substr($number, 0, 4) . str_repeat($maskingCharacter, strlen($number) - 8) . substr($number, -4);
}
您可以使用substr_replace
$var = '1234123412341234';
$var = substr_replace($var, str_repeat("X", 8), 4, 8);
echo $var;
输出
1234XXXXXXXX1234
<?php
echo 'XXXX-XXXX-XXXX-'.substr($cc,-4);
?>
我的 5 美分。
示例:
371449635398431 => XXX-XXXX-XXXX-8431
4111111111111111 => XXXX-XXXX-XXXX-1111
public function maskCreditCardNumber($cc, $maskFrom = 0, $maskTo = 4, $maskChar = 'X', $maskSpacer = '-')
{
// Clean out
$cc = str_replace(array('-', ' '), '', $cc);
$ccLength = strlen($cc);
// Mask CC number
if (empty($maskFrom) && $maskTo == $ccLength) {
$cc = str_repeat($maskChar, $ccLength);
} else {
$cc = substr($cc, 0, $maskFrom) . str_repeat($maskChar, $ccLength - $maskFrom - $maskTo) . substr($cc, -1 * $maskTo);
}
// Format
if ($ccLength > 4) {
$newCreditCard = substr($cc, -4);
for ($i = $ccLength - 5; $i >= 0; $i--) {
// If on the fourth character add the mask char
if ((($i + 1) - $ccLength) % 4 == 0) {
$newCreditCard = $maskSpacer . $newCreditCard;
}
// Add the current character to the new credit card
$newCreditCard = $cc[$i] . $newCreditCard;
}
} else {
$newCreditCard = $cc;
}
return $newCreditCard;
}
假如说:
这就是我所做的:
代码是这样的:
public function mask($string)
{
$regex = '/(?:\d[ \t-]*?){13,19}/m';
$matches = [];
preg_match_all($regex, $string, $matches);
// No credit card found
if (!isset($matches[0]) || empty($matches[0]))
{
return $string;
}
foreach ($matches as $match_group)
{
foreach ($match_group as $match)
{
$stripped_match = preg_replace('/[^\d]/', '', $match);
// Is it a valid Luhn one?
if (false === $this->_util_luhn->isLuhn($stripped_match))
{
continue;
}
$card_length = strlen($stripped_match);
$replacement = str_pad('', $card_length - 4, $this->_replacement) . substr($stripped_match, -4);
// If so, replace the match
$string = str_replace($match, $replacement, $string);
}
}
return $string;
}
您将看到对 $this->_util_luhn->isLuhn 的调用,这是一个执行此操作的函数:
public function isLuhn($input)
{
if (!is_numeric($input))
{
return false;
}
$numeric_string = (string) preg_replace('/\D/', '', $input);
$sum = 0;
$numDigits = strlen($numeric_string) - 1;
$parity = $numDigits % 2;
for ($i = $numDigits; $i >= 0; $i--)
{
$digit = substr($numeric_string, $i, 1);
if (!$parity == ($i % 2))
{
$digit <<= 1;
}
$digit = ($digit > 9)
? ($digit - 9)
: $digit;
$sum += $digit;
}
return (0 == ($sum % 10));
}
这就是我在https://github.com/pachico/magoo/中实现它的方式。希望你觉得它有用。
带正则表达式
function cc_masking( $number, $maskChar = 'X' ) {
return preg_replace(
'/^(....).*(....)$/',
'\1' . str_repeat( $maskChar, strlen( $number ) - 8) . '\2',
$number );
}
您保留前四个字符(和后四个字符),用 X 替换其他字符。
如果您只想显示最后 4 位数字,这是一种动态方式。适用于信用卡、ACH 号码或任何东西:
https://gist.github.com/khoipro/815ea292e2e87e10771474dc2ef401ef
$variable = '123123123';
$length = strlen($variable);
$output = substr_replace($variable, str_repeat('X', $length - 4), 0, $length - 4);
echo $output;
不需要正则表达式。只需在开头取n个数字,在结尾取n个数字,然后在中间加上X即可完成。
不是优雅的方式,但它有效
<?php
$cc = "1234123412341234";
function cc_masking($number) {
$int_first = 4;
$int_last = 4;
$chars = strlen($number);
$repeater = "x";
$repeates = $chars-$int_first-$int_last;
// echo "<p>Org: $number</p>";
$mask = substr($number,0,4).str_repeat($repeater,$repeates).substr($cc,-4);
// echo "<p>Mask: $mask";
return $mask;
}
echo cc_masking($cc);
?>
我正在寻找一些类似的代码。所以我找到了下面的代码。我希望能有所帮助。
function mask($val, $mask)
{
$maskared = '';
$k = 0;
for ($i = 0; $i <= strlen($mask) - 1; $i++) {
if ($mask[$i] == '#') {
if (isset($val[$k]))
$maskared .= $val[$k++];
} else {
if (isset($mask[$i]))
$maskared .= $mask[$i];
}
}
return $maskared;
}
样本:
echo mask(11111111111, "###.###.###-##"); // 111.111.111-11
echo mask(11111111111, "#####-###"); // 111111-111
echo mask(11111111111, anything); // 111111-111
str_pad(substr($value,-4),strlen($value), "X", STR_PAD_LEFT); //output: XXXXXXXXXXXX9494
$accNum = "1234123412341234";
$accNum1 = substr($accNum,2,2);
$accNum1 = '**'.$accNum1;
$accNum2 = substr($accNum,6,100);
$accNum2 = '**'.$accNum2;
$accNum = $accNum1.$accNum2;
echo $accNum;