我有一个脚本,其中包含许多信用卡CVV编号,并且我希望能够搜索CVV单词并在其后仅替换CVV编号。这是脚本文档。
对不起,我应该在全球范围内提及。因此,例如,如果我要在同一文档/脚本中再提及CVV,并在其旁边再说其他字母或不必要的字符,则我需要一种方法来仅过滤CVV和数字,然后使用正则表达式掩盖数字本身。
我看了很多视频并搜索了正则表达式文档的分配,但我似乎无法弄清。
<?php
function displayInfo(){
$ccPattern = "/(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9]
[0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|
(?:2131|1800|35\d{3})\d{11})/";
$cvvPattern = "/(cvv\W{0,5}\w{0,12})(\d{0,3})/";
$ExpPattern = "//";
$nsInfo = "orderId : 212939129" . "<br>" .
"orderNumber : INV10001" . "<br>" .
"salesTax : 1.00" . "<br>" .
"amount : 21.00" . "<br>" .
"terminal : 5" . "<br>" .
"currency : 1" . "<br>" .
"type : purchase" . "<br>" .
"avsStreet : 123 Road" . "<br>" .
"avsZip : A1A 2B2" . "<br>" .
"customerCode : CST1001" . "<br>" .
"cardId : 18951828182" . "<br>" .
"cardHolderName : John Smith" . "<br>" .
"cardNumber : 5454545454545454" . "<br>" .
"cardExpiry : 1025" . "<br>" .
"cardCVV : 100";
$maskcc = preg_replace($ccPattern, "****************", $nsInfo);
$maskCVV = preg_replace($cvvPattern, "***", $nsInfo);
echo $maskcc;
echo $maskCVV;
}
displayInfo();
?>
答案 0 :(得分:1)
如果仅在cardCVV之后用***替换数字,则可以将preg_replace与:
\bcardCVV\h+:\h+\K\d+
说明
\bcardCVV
单词边界和匹配cardCVV h+:\h+
将:
与水平空白字符匹配1次以上\K
忘记匹配的内容\d+
匹配1个以上的数字$re = '/\bcardCVV\h+:\h+\K\d+/';
$nsInfo = "orderId : 212939129" . "<br>" .
"orderNumber : INV10001" . "<br>" .
"salesTax : 1.00" . "<br>" .
"amount : 21.00" . "<br>" .
"terminal : 5" . "<br>" .
"currency : 1" . "<br>" .
"type : purchase" . "<br>" .
"avsStreet : 123 Road" . "<br>" .
"avsZip : A1A 2B2" . "<br>" .
"customerCode : CST1001" . "<br>" .
"cardId : 18951828182" . "<br>" .
"cardHolderName : John Smith" . "<br>" .
"cardNumber : 5454545454545454" . "<br>" .
"cardExpiry : 1025" . "<br>" .
"cardCVV : 100";
$result = preg_replace($re, "***", $nsInfo);
echo $result;
结果
orderId : 212939129
orderNumber : INV10001
salesTax : 1.00
amount : 21.00
terminal : 5
currency : 1
type : purchase
avsStreet : 123 Road
avsZip : A1A 2B2
customerCode : CST1001
cardId : 18951828182
cardHolderName : John Smith
cardNumber : 5454545454545454
cardExpiry : 1025
cardCVV : ***
答案 1 :(得分:0)