This method masks credit card numbers and shows only the last 4 digits. I also forgot where I got this method from. If you wish to change the number of last digits shown, just modify the last parameter of the method. The part where it says $l = 4.

function maskCC($n, $b = 4, $p = 16, $m = 'X', $l = 4) {
  // $n = 'card number: 1234567890123456' string
  // $b = break into how many in each chunk?
  // $p = pad to what length?
  // $m = mask character?
  // $l = leave how many numbers showing?
  preg_match("/card number:\s*(\d+)[^\d]*/i", trim($n), $matches);
  $pattern = "/card number:\s*(\d+)/ie";
  $replacement = " implode('-', str_split( substr_replace( str_pad('$1', '$p', '0', STR_PAD_LEFT), str_repeat('$m', $p-$l), 0, $l * -1 ), $b) ); ";
  return preg_replace($pattern, $replacement, trim($n));
}

// use the function
var_dump(maskCC("\ncard number: 1222233334444\n"));

Related Posts Plugin for WordPress, Blogger...