How to hide some portion of phone number or partially hide phone number in PHP. This blog all about, makes privacy to show a phone number for unauthenticated users. This could be easily done by a single method in using PHP in-built function like strlen() and substr().
strlen() count the character of string.
substr() helps to extract a part of string.
Here we can take help of these two methods to obtain our result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php $phone='9542424242'; function cross($phone) { $times=strlen(trim(substr($phone,2,6))); $cross=''; for ($i=0; $i <$times ; $i++) { $cross.='x'; } return $cross; } $result=str_replace(substr($phone, 2,6), cross($phone), $phone); echo $result; ?> Output:95xxxxxx42 |