简体   繁体   English

如何在脚本中找到所有提及 CVV 的数字,然后用 * 替换数字。 在 PHP 中使用正则表达式

[英]how to find all mentions of CVV with number in script, then replace just number with *. using regex with PHP

I have a script that has many credit card CVV numbers in them, and i want to be able to search for the word CVV and replace just the CVV number after it.我有一个脚本,其中包含许多信用卡 CVV 编号,我希望能够搜索单词 CVV 并仅替换其后的 CVV 编号。 this is the script document.这是脚本文件。

Sorry, I should have mentioned globally.抱歉,我应该在全球范围内提及。 so if for instance I was to have any more mentions of a CVV in the same document/script with say other letters or unnecessary characters beside it, i need a way to filter out just CVV and the number then mask the number itself using regex.因此,例如,如果我要在同一文档/脚本中再提及 CVV,并在其旁边说其他字母或不必要的字符,我需要一种方法来过滤掉 CVV 和数字,然后使用正则表达式屏蔽数字本身。

I have watched many videos and searched up allot of regex documentation and I just can't seem to figure it out.我看过很多视频并搜索了大量的正则表达式文档,但我似乎无法弄清楚。

<?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();

?>

If you want to replace the digits only after cardCVV with *** you might use preg_replace with:如果你只想用 *** 替换 cardCVV 之后的数字,你可以使用preg_replace

\bcardCVV\h+:\h+\K\d+

Explanation解释

  • \\bcardCVV Word boundary and match cardCVV \\bcardCVV字边界并匹配 cardCVV
  • h+:\\h+ Match : between 1+ times a horizontal whitespace character h+:\\h+匹配:在 1+ 倍的水平空白字符之间
  • \\K Forget what was matched \\K忘记匹配的内容
  • \\d+ Match 1+ digits \\d+匹配 1+ 个数字

Regex demo正则表达式演示

$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;

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 : ***

Use:用:

cardCVV : (\d+)

and 100 will be in the $1 position. 100将位于$1位置。

https://regex101.com/r/4LSLBy/1/ https://regex101.com/r/4LSLBy/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM