我有信用卡号码,我想屏蔽如下:
$cc = 1234123412341234
echo cc_masking($cc)
1234XXXXXXXX1234
function cc_masking($number) {.....}
请为此建议正则表达式。
答案 0 :(得分:34)
这应该可以使用substr
:
function ccMasking($number, $maskingCharacter = 'X') {
return substr($number, 0, 4) . str_repeat($maskingCharacter, strlen($number) - 8) . substr($number, -4);
}
答案 1 :(得分:17)
您可以使用substr_replace
$var = '1234123412341234';
$var = substr_replace($var, str_repeat("X", 8), 4, 8);
echo $var;
输出
1234XXXXXXXX1234
答案 2 :(得分:11)
<?php
echo 'XXXX-XXXX-XXXX-'.substr($cc,-4);
?>
答案 3 :(得分:2)
假设:
这就是我的所作所为:
代码是这样的:
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-&gt; _util_luhn-&gt; 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/中实现它的方式。 希望你觉得它很有用。
答案 4 :(得分:1)
我5美分。
示例:
371449635398431 =&gt; XXX-XXXX-XXXX-8431
4111111111111111 =&gt; 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;
}
答案 5 :(得分:0)
不需要正则表达式。只需在开头取n个数字,最后取n个数字,然后在中间加上X来完成。
答案 6 :(得分:0)
不是优雅的方式,但它有效
<?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);
?>
答案 7 :(得分:0)
使用正则表达式
function cc_masking( $number, $maskChar = 'X' ) {
return preg_replace(
'/^(....).*(....)$/',
'\1' . str_repeat( $maskChar, strlen( $number ) - 8) . '\2',
$number );
}
保留前四个字符(和后四个字符),用X替换其他字符。
答案 8 :(得分:0)
如果只想显示最后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;
答案 9 :(得分:0)
我正在寻找一些类似的代码。所以我找到了下面的代码。希望能对您有所帮助。
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
答案 10 :(得分:-1)
$accNum = "1234123412341234";
$accNum1 = substr($accNum,2,2);
$accNum1 = '**'.$accNum1;
$accNum2 = substr($accNum,6,100);
$accNum2 = '**'.$accNum2;
$accNum = $accNum1.$accNum2;
echo $accNum;