Hide numbers of a phone number

You can use substr_replace() function

<?php
$mobnum ="09350943256";
for($i=4;$i<7;$i++)
{
  $mobnum = substr_replace($mobnum,"*",$i,1);
}
echo $mobnum;
?>

You could use substr and concat this way

to work for any $number with any number of n digit length

 <?php

     $number = "112222";
     $middle_string ="";
     $length = strlen($number);

     if( $length < 3 ){

       echo $length == 1 ? "*" : "*". substr($number,  - 1);

     }
     else{
        $part_size = floor( $length / 3 ) ; 
        $middle_part_size = $length - ( $part_size * 2 );
        for( $i=0; $i < $middle_part_size ; $i ++ ){
           $middle_string .= "*";
        }

        echo  substr($number, 0, $part_size ) . $middle_string  . substr($number,  - $part_size );
     }

The output if you make $number = "1" is * and if $number = "12" is *2 and for $number = "112222" is 11**22. and it goes on.


In short:

$phone = 01133597084;
$maskedPhone = substr($phone, 0, 4) . "****" . substr($phone, 7, 4);

// Output: 0113****7084

Tags:

Php