In this post, I will tell you how to replace characters with a special character in PHP. In a rare scenario, we need to hide particular information from original for security purposes.
For example, when we made any transaction bank will send SMS with your first and last few digits for account numbers with Debit amount details.
Another example, whenever we lost or forget the profile login credential then we get or change the particular details with the help of registered e-mail or phone no. In that time the system will ask you to type exact details what you provided at the time of registration. In that area, the system will show your details with some special character as a note.
Here we are using str_repeat() and substr() methods to perform the task.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php echo "<b>Email Id hiding : </b>"; $secure_mail[0] = str_repeat("*", strlen($secure_mail[0])); echo implode("@", $secure_mail); echo "<br/> <br/> Phone No hiding with # : " . substr("1234567890", 0, -4) . "####"; $original_string = "Learn Infinity"; $str_length = strlen($original_string); echo "<br/> <br/> Some String hiding with - : " .substr($original_string, 0, 3).str_repeat('-', $str_length - 3).substr($original_string, $str_length - 4, 4); ?> |