简体   繁体   中英

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. 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.

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:

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

Explanation

  • \\bcardCVV Word boundary and match cardCVV
  • h+:\\h+ Match : between 1+ times a horizontal whitespace character
  • \\K Forget what was matched
  • \\d+ Match 1+ digits

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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