Mask credit card number in PHP

前端 未结 11 1555
执笔经年
执笔经年 2020-12-09 13:19

I have credit card number which I want to mask as below:

$cc = 1234123412341234

echo cc_masking($cc)

1234XXXXXXXX1234

function cc_masking($number) {.....}         


        
11条回答
  •  旧时难觅i
    2020-12-09 13:53

    If you wish to show only last 4 digits, here is a dynamic way. Works with both credit cards, ACH numbers or anything:

    https://gist.github.com/khoipro/815ea292e2e87e10771474dc2ef401ef

        $variable = '123123123';
        $length = strlen($variable);
        $output = substr_replace($variable, str_repeat('X', $length - 4), 0, $length - 4);
        echo $output;
    

提交回复
热议问题