Skip to content

Instantly share code, notes, and snippets.

@marcus-at-localhost
Last active August 29, 2015 14:21
Show Gist options
  • Save marcus-at-localhost/aca157b7a1d2d13e37d2 to your computer and use it in GitHub Desktop.
Save marcus-at-localhost/aca157b7a1d2d13e37d2 to your computer and use it in GitHub Desktop.
Partial string replacement to anonymize credit card numbers, bank account numbers etc
/**
* Replace middle of the string with given char, keeping length
* http://mamchenkov.net/wordpress/2011/05/17/partial-string-replacement-with-fixed-length-in-php/
*
* <code>
* print padString('1234567890123456', 1, 4, 'x');
* </code>
*
* @param string $string String to process
* @param integer $start Characters to skip from start
* @param integer $end Characters to leave at the end
* @param string $char Character to replace with
* @return string
*/
function padString($string, $start, $end, $char) {
$result = '';
$length = strlen($string) - $start - $end;
if ($length <= 0) {
$result = $string;
}
else {
$replacement = sprintf("%'{$char}" . $length . "s", $char);
$result = substr_replace($string, $replacement, $start, $length);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment