Skip to content

Instantly share code, notes, and snippets.

@bolechen
Last active January 20, 2021 02:16
Show Gist options
  • Save bolechen/3e7a5bca03f96a8be76b77e8081dfd0c to your computer and use it in GitHub Desktop.
Save bolechen/3e7a5bca03f96a8be76b77e8081dfd0c to your computer and use it in GitHub Desktop.
Add mask for Laravel Str helper
<?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