0

I am trying to hide part of phone number of my users to prevent it from phone call spam.

My code displays full user phone number like: 510 200 300 and what i want to achive is to hide most letters with "x" like so: 51X XXX XXX. Later on i plan to let real user to click on it to show full number.

My code is:

function maskPhoneNumber($number){

$number = the_author_meta( 'phone_number', $author_id ); // get user phone number from database
$mask_number =  str_repeat("*", strlen($number)-4) . substr($number, -4);
return $mask_number;

}
echo maskPhoneNumber($number);

However it returns nothing. I tried many configurations, but nothing works, please help.

epascarello
  • 204,599
  • 20
  • 195
  • 236
bajcepsme
  • 87
  • 10

1 Answers1

0
function maskPhoneNumber($number,$digitsToHide){
    $number = the_author_meta( 'phone_number', $author_id ); // get user phone number from database
    $len = strlen($number);
    for($i = ($len-$digitsToHide)-1; $i<$len; $i++){
        $number[$i] = "*";
    }
    return $number;
}
garry man
  • 445
  • 4
  • 14
  • and still `$author_id` will be undefined and so there should be no result – Sindhara Mar 15 '19 at 20:41
  • 1
    The question is how to hide part of a phone number. He didn't share the rest of the code so it's unkown whether `$author_id` is a global variable or a function scope one. – garry man Mar 15 '19 at 20:46
  • My script knows the value of $author_id, becouse it returns correct phone number for each user. So $author_id is known. The problem is i can't make script to show only part of this number. – bajcepsme Mar 15 '19 at 21:42
  • And i tagged this post with javascript because i thought it's the best way to show and hide rest of the hidden phone number with a click. – bajcepsme Mar 15 '19 at 21:43
  • @bajcepsme have you tried my code? – garry man Mar 15 '19 at 21:43
  • @garry man, yes i tried, but still nothing shows :( – bajcepsme Mar 15 '19 at 21:53
  • @bajcepsme try debugging the phone number value before `maskPhoneNumber` function call and after, see what changes. – garry man Mar 15 '19 at 22:05
  • Still nothing. It seems like function can't read $author_id indeed. Even though i used global varialbe. – bajcepsme Mar 15 '19 at 22:12
  • Please take a look here: http://php.net/manual/en/language.variables.scope.php – garry man Mar 16 '19 at 11:33