Skip to content

Instantly share code, notes, and snippets.

@egulhan
Last active April 30, 2020 11:22
Show Gist options
  • Save egulhan/9e96aaea52094c10d706225ec94e4d6a to your computer and use it in GitHub Desktop.
Save egulhan/9e96aaea52094c10d706225ec94e4d6a to your computer and use it in GitHub Desktop.
Some string masking functions using PHP
<?php
function maskString($str)
{
$result = '';
$parts = explode(' ', $str);
foreach ($parts as $part) {
if (!empty($part)) {
if (strlen($part) == 1) {
$result .= '*';
} else {
$result .= mb_substr($part, 0, 1);
$result .= str_repeat('*', strlen($part) - 1);
$result .= ' ';
}
}
}
if (empty($result)) {
return $str;
}
if (strlen($result) > 1) {
// remove the last space char
$result = substr($result, 0, strlen($result) - 1);
}
return $result;
}
function maskEmailAddress($email)
{
$parts = explode('@', $email);
if (count($parts) <= 1) {
return $email;
}
return maskString($parts[0]) . '@' . $parts[1];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment