onlinecode

How To Replace Some Characters With Asterisks in PHP

How To Replace Some Characters With Asterisks in PHP

In this post we will give you information about How To Replace Some Characters With Asterisks in PHP. Hear we will give you detail about How To Replace Some Characters With Asterisks in PHPAnd how to use it also give you demo for it if it is necessary.

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 :

  1. <?php
  2. functionhide_mail($email){
  3. $mail_part=explode("@",$email);
  4. $mail_part[]=str_repeat("*",strlen($mail_part[]));
  5. returnimplode("@",$mail_part);
  6. }
  7. echohide_mail("info@onlinecode");
<?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@onlinecode");



Replace Some Characters With asterisks for phone numbers :

  1. <?php
  2. functionhide_mobile($mobile){
  3. returnsubstr($mobile,,-4)."****";
  4. }
  5. echohide_mobile("9999999999");
<?php
function hide_mobile($mobile) {
    return substr($mobile, 0, -4) . "****";
}

echo hide_mobile("9999999999");



Replace all characters with asterisks except first and last characters :

  1. <?php
  2. functionget_starred($str){
  3. $str_length=strlen($str);
  4. returnsubstr($str,,1).str_repeat('*',$str_length-2).substr($str,$str_length-1,1);
  5. }
  6. $my_string='Yourname';
  7. echoget_starred($my_string);//will give you output Y******e
<?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 



PHP provide so many helper function and now you can remove characters within a portion of string.

Label :

PHP

How To

Hope this code and post will helped you for implement How To Replace Some Characters With Asterisks in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version