Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide numbers of a phone number

Tags:

php

How can I show and hide the some numbers of a phone number by replacing it with * like 0935***3256 by PHP?

EX:

09350943256 -> 0935***3256
09119822432 -> 0911***2432
09215421597 -> 0921***1597
...


$number = '09350943256';
echo str_pad(substr($number, -4), strlen($number), '*', STR_PAD_LEFT);

Top php code result is as: *******3256 but i want result as: 0935***3256

How is it?

like image 653
Me hdi Avatar asked Feb 14 '17 14:02

Me hdi


People also ask

Can you hide a phone number?

Use *67 to hide your phone number Open your phone's keypad and dial * - 6 - 7, followed by the number you're trying to call. The free process hides your number, which will show up on the other end as “Private” or “Blocked” when reading on caller ID.

Does * 67 hide numbers?

You have the option to block Caller ID either temporarily or permanently. To block your number from being displayed temporarily for a specific call: Enter *67. Enter the number you wish to call (including area code).

What does * 69 do on a phone?

Call return (*69) automatically dials your last incoming call, whether the call was answered, unanswered or busy. Call within 30 minutes, during which you can still make and receive calls. To deactivate while waiting for the party you are trying to reach to become available, dial *89.


2 Answers

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.

like image 137
ScaisEdge Avatar answered Sep 20 '22 11:09

ScaisEdge


In short:

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

// Output: 0113****7084

like image 45
Jaber Al Nahian Avatar answered Sep 17 '22 11:09

Jaber Al Nahian