views:

61

answers:

2

Trying to replace the first 12 digits of credit card numbers with X's in a predictable blob of text that contains the string:

Credit Card Number: 1234123412341234

Here's my PHP function:

preg_replace('/Credit Card Number: ([0-9]{12})/','Credit Card Number: XXXXXXXXXXXX',$str);

Help?

+2  A: 

I don't see what's wrong. Perhaps you forgot to assign the result?

$str = preg_replace('/Credit Card Number: [0-9]{12}/','Credit Card Number: XXXXXXXXXXXX',$str);

Also, the capturing around ([0-9]{12}) is unnecessary if you don't need it.

KennyTM
The reason I put that there is because it was looking for "Credit Card Number: 1" 16 times in a row, rather than just the digit 16 times in row. It was matching the whole pattern 16 times, in other words.The problem right now is that there's no match. :(
Aaron
@Aaron: Then you have some misplaced parenthesis. The `{12}` only applies on the pattern before it, i.e. `[0-9]`, not the whole pattern, unless you write `/(Credit Card Number: [0-9]){12}/`.
KennyTM
+1  A: 

Dumb question: you are assigning the returned value back to $str right?

$str = preg_replace('/(Credit Card Number: [0-9]{12}/','Credit Card Number: XXXXXXXXXXXX',$str);
publicRavi
That's it! Thanks a million.
Aaron
I lol'd. Sometimes it happens to me too :(
M28
Actually KennyTM asked the same question first. +1. Guess I was lucky :)
publicRavi