Skip to content
Advertisement

Hide last digits of phone number like 99XX XXX XXX PHP or jQuery

I have this code that outputs the var $phone.

<span class="row_value phone"><?php echo $this->items->phone; ?></span>

Which outputs: 9999 999 999. I want it to output 99XX XXX XXX and display a link below the phone number which say “show phone number” and on click to turn into 9999 999 999.

Advertisement

Answer

Try this….

PHP

<?php
    $ph  = $this->items->phone;
    $ph1 = substr($ph,0,2);
    $ph2 = substr($ph,1,strlen($ph));
?>
    <span class="row_value phone"><?php echo $ph1; ?><span id="phone-x" data-data="<?php echo $ph2; ?>"><?php echo preg_replace("/[0-9]/","X",$ph2); ?></span></span>
    <button id="show_ph">show phone number</button>

jQuery

$(document).ready(function() {
  $("#show_ph").click(function() {
    var data = $("#phone-x").html();
    var adata = $("#phone-x").attr("data-data");        
    $("#phone-x").html(adata);
    $("#phone-x").attr("data-data",data);
  })
})

Example

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement