Skip to content

Instantly share code, notes, and snippets.

@khoipro

khoipro/helper.php

Last active Apr 19, 2019
Embed
What would you like to do?
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