@@ -1094,16 +1094,16 @@ static public function imageToBase64($path = null, $url = false)
10941094 }
10951095
10961096 /**
1097- * Masks characters in a string.
1097+ * Masks characters in a string while keeping a specified number of visible characters .
10981098 *
10991099 * @param string|null $str
11001100 * - The string to be masked.
11011101 *
11021102 * @param int $length
1103- * - The desired length of the masked string . Default is 4.
1103+ * - The number of visible characters . Default is 4.
11041104 *
11051105 * @param string $position
1106- * - The position to apply the mask : 'left', 'center', 'right'. Default is 'right'.
1106+ * - The position to keep visible : 'left', 'center', 'right'. Default is 'right'.
11071107 *
11081108 * @param string $mask
11091109 * - The character used for masking. Default is '*'.
@@ -1118,69 +1118,52 @@ static public function mask($str = null, ?int $length = 4, ?string $position = '
11181118 return $ str ;
11191119 }
11201120
1121- // trim string
1121+ // Trim string and position input
11221122 $ str = Str::trim ($ str );
11231123 $ position = Str::trim ($ position );
11241124
11251125 // Get the length of the string
11261126 $ strLength = mb_strlen ($ str , 'UTF-8 ' );
11271127
1128+ // If length is greater than or equal to the string length, return the original string (nothing to mask)
1129+ if ($ length >= $ strLength ) {
1130+ return $ str ;
1131+ }
1132+
1133+ // Calculate the number of masked characters
1134+ $ maskedLength = max (0 , $ strLength - $ length );
1135+
11281136 // Check if it's an email by finding the last occurrence of "@"
11291137 $ atPosition = mb_strrpos ($ str , "@ " , 0 , 'UTF-8 ' );
1130-
1131- // Check if it's an actual email
11321138 $ isEmail = self ::emailValidator ($ str , false , false );
11331139
1134- // Convert length to integer
1135- $ length = (int ) $ length ;
1136-
1137- // Ensure the mask length does not exceed the string length
1138- $ length = min ($ length , $ strLength );
1139-
1140- // is length and string length is same, then divide num into two
1141- if ($ strLength === $ length ){
1142- $ length = (int ) ceil ($ length / 2 );
1143- }
1144-
1145- // Calculate number of unmasked characters
1146- $ unmaskedLength = $ strLength - $ length ;
1147-
1148- // if valid email address and position of email is found
1140+ // If it's a valid email, mask only the email part (excluding the domain)
11491141 if ($ isEmail && $ atPosition !== false ) {
1150- // extract email without the tld (top-level domain)
11511142 $ email = mb_substr ($ str , 0 , mb_strpos ($ str , "@ " ));
1152-
1153- // extract only the tld, to be added at the end
11541143 $ tld = mb_substr ($ str , mb_strpos ($ str , "@ " ));
11551144
1156- // now mask only the email using the middle position
1157- $ maskedString = self ::mask ($ email , $ length , $ position );
1158-
1159- return "{$ maskedString }{$ tld }" ;
1145+ // Mask only the email part, keeping visibility as per the $length
1146+ $ maskedEmail = self ::mask ($ email , $ length , $ position );
1147+ return "{$ maskedEmail }{$ tld }" ;
11601148 }
11611149
1162- // For left position
1163- if ($ position == 'left ' ) {
1164- // Mask the first 'length' characters and leave the rest visible
1165- return str_repeat ($ mask , $ length ) . mb_substr ($ str , $ length , null , 'UTF-8 ' );
1150+ // Left masking: Show first 'length' characters, mask the rest
1151+ if ($ position === 'left ' ) {
1152+ return mb_substr ($ str , 0 , $ length , 'UTF-8 ' ) . str_repeat ($ mask , $ maskedLength );
11661153 }
1167- elseif ( $ position == ' right ' ) {
1168- // Mask the right part of the string
1169- return mb_substr ( $ str , 0 , $ unmaskedLength , ' UTF-8 ' ) . str_repeat ( $ mask , $ length );
1154+ // Right masking: Mask everything except the last 'length' characters
1155+ elseif ( $ position === ' right ' ) {
1156+ return str_repeat ( $ mask , $ maskedLength ) . mb_substr ( $ str , - $ length, null , ' UTF-8 ' );
11701157 }
1158+ // Center masking: Keep equal parts visible on both sides
11711159 else {
1172- // Calculate how much to keep visible on both sides
1173- $ halfLength = (int ) floor (($ strLength - $ length ) / 2 ); // Evenly distribute the mask
1174-
1175- // Ensure the remaining visible part accounts for the total length correctly
1176- $ start = mb_substr ($ str , 0 , $ halfLength );
1177- $ end = mb_substr ($ str , - $ halfLength );
1178-
1179- // Mask the middle portion
1180- return $ start . str_repeat ($ mask , $ length ) . $ end ;
1160+ $ halfVisible = (int ) floor ($ length / 2 );
1161+ $ start = mb_substr ($ str , 0 , $ halfVisible , 'UTF-8 ' );
1162+ $ end = mb_substr ($ str , -$ halfVisible , null , 'UTF-8 ' );
1163+ return $ start . str_repeat ($ mask , $ maskedLength ) . $ end ;
11811164 }
11821165 }
1183-
1166+
11841167 /**
11851168 * Validate an email address.
11861169 *
0 commit comments