In this post, i will tell you how to remove some characters with asterisks in PHP.
In some situation you want to hide some characters for security reasons.
For example, if you ever noticed you receive email from bank then you will see some characters of your bank account is removed with asterisks or with any other special characters.
Other example, when you withdraw some amount from atm then you will get a reciept against transaction then you will see they don't show full card number on reciept because of some security reasons and make you safe from scammers.
In this post, i will give you three example :
Replace Some Characters With asterisks for mails :
- <?php
- function hide_mail($email) {
- $mail_part = explode("@", $email);
- $mail_part[0] = str_repeat("*", strlen($mail_part[0]));
- return implode("@", $mail_part);
- }
- echo hide_mail("info@expertphp.in");
- <?php
- function hide_mobile($mobile) {
- return substr($mobile, 0, -4) . "****";
- }
- echo hide_mobile("9999999999");
- <?php
- function get_starred($str) {
- $str_length = strlen($str);
- return substr($str, 0, 1).str_repeat('*', $str_length - 2).substr($str, $str_length - 1, 1);
- }
- $my_string = 'Yourname';
- echo get_starred($my_string); //will give you output Y******e