Skip to content

Instantly share code, notes, and snippets.

@khoipro
Last active April 19, 2019 16:06
Show Gist options
  • Select an option

  • Save khoipro/815ea292e2e87e10771474dc2ef401ef to your computer and use it in GitHub Desktop.

Select an option

Save khoipro/815ea292e2e87e10771474dc2ef401ef to your computer and use it in GitHub Desktop.
Mask credit card, account number, etc... with XXXX, show just last 4 last strings/digits.
<?php
/**
* You can quick test with http://phptester.net/
**/
$account_number = '123123123';
$credit_card_number = '4242424242424242';
$account_length = strlen($account_number);
$credit_card_length = strlen($credit_card_number);
$account_output = substr_replace($account_number, str_repeat('X', $account_length - 4), 0, $account_length - 4);
echo '<pre>';
echo $account_output; // XXXXX3123
echo '</pre>';
$credit_card_output = substr_replace($credit_card_number, str_repeat('X', $credit_card_length - 4), 0, $credit_card_length - 4);
echo '<pre>';
echo $credit_card_output; // XXXXXXXXXXXX4242
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment