masking phone number on output

Discussion in 'PHP' started by mattjfrank, Jul 26, 2007.

  1. #1
    I can provide actual website and info if needed..

    I am pulling a "text" out of mysql with php. Within this text is a formatted phone number formatted 123-456-7890 what i want to do is make it have all the text in tact but graph that bit and change it to 123-456-XXXX and hide the phone number.

    Anyone???
     
    mattjfrank, Jul 26, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    function mask_num($number)
    {
    	return preg_replace('/\d{4}$/', 'XXXX', $number);
    }
    
    PHP:
    
    $number = mask_num($number);
    
    PHP:
     
    nico_swd, Jul 27, 2007 IP
  3. mattjfrank

    mattjfrank Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Awesome thanks, this works. Well most of the time. Could you explain to me how it works so I can figure out why sometimes it only catches one of the phone numbers and why sometimes it doesn't catch it at all?

    I know the code mostly it's just this '/\d{4}$/' i'm not sure exactly what it all means.

    edit
    ** just to be clear when a phone number has a , or . after it then it doesn't mask. also when there are two sets of phone numbers it's only masks one set.
     
    mattjfrank, Jul 31, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Can you post an example of the whole text that is pulled from the database? And also all possible number formats.
     
    nico_swd, Aug 1, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    This should be fine:
    preg_replace('/(\d{3}-\d{3}-)\d{4}/', '$1XXXX', $number);
    PHP:
    Or maybe even this:
    preg_replace('/\d{4}/', 'XXXX', $number);
    PHP:
    Depends on how much other data is in the text.
     
    krt, Aug 1, 2007 IP
  6. mattjfrank

    mattjfrank Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks to both of you
    
    preg_replace('/(\d{3}-\d{3}-)\d{4}/', '$1XXXX', $number); seems to of done it
    PHP:
     
    mattjfrank, Aug 1, 2007 IP