Last active
January 20, 2021 02:16
-
-
Save bolechen/3e7a5bca03f96a8be76b77e8081dfd0c to your computer and use it in GitHub Desktop.
Add mask for Laravel Str helper
This file contains 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 | |
// abcde -> a*e | |
Str::macro('mask', function ($subject, $maxlength = 5, $mask_symbol = '*'): string { | |
$subject = mb_substr($subject, 0, $maxlength); | |
$length = mb_strlen($subject, 'utf-8'); | |
if ($length < 2) { | |
return $subject; | |
} | |
$firstStr = mb_substr($subject, 0, 1, 'UTF-8'); | |
$lastStr = mb_substr($subject, -1, 1, 'UTF-8'); | |
return $firstStr.$mask_symbol.$lastStr; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment