Page 1 of 1

Credit Card Last 4 Digits

Posted: Thu Nov 09, 2006 8:14 pm
by chas688
I'm building a site where I will be passing credit card information to a clearing house, but I want to retain the last four digits of the credit card number in the database and associate it with the order.

Does any more experienced in php than myself know of the best way to do this? Should I just use string functions to replace the digits?

Has anyone already created a function to validate the cc number and then replace all the digits except the last four with asterisks?

Thanks. :D

Posted: Thu Nov 09, 2006 8:26 pm
by Burrito
use substr() to fetch the last four then put X's in front of them.

ie:

Code: Select all

$lastfour = substr($_POST['ccnum'],-4);
$hiddencc = "xxx...xx$lastfour";

Thanks!

Posted: Thu Nov 09, 2006 8:28 pm
by chas688
I would have made it much more complex. Thanks for the guidance.

Chas688

Posted: Fri Nov 10, 2006 9:55 am
by Jenk
Just for fun (I'm bored) you can use the str_pad function, too:

Code: Select all

<?php

$maskedNumber =  str_pad(substr($number, -4), strlen($number), '*', STR_PAD_LEFT);

?>