Last active
April 30, 2020 11:22
-
-
Save egulhan/9e96aaea52094c10d706225ec94e4d6a to your computer and use it in GitHub Desktop.
Some string masking functions using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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