-1

Hi there i am trying to hide the customers name and i wants to hide only the last 4 with asterisks currently i have this code but it hides the entire name after 4 digits but thats not what i want

$arr[$idx]['consignee']  = $row['consignee'];

i managed to hide the name using this code below but it hides all after 4

            $arr[$idx]['consignee'] = '1' > 0 ?
                                    sub_str($row['consignee'], '5') : $row['consignee'];

Any sample or suggestion please.

1 Answers1

1

Like this

 $arr[$idx]['consignee'] = preg_replace('/.{4}$/', '****',  $arr[$idx]['consignee']);

Try it here online

OR substr ( for those that think regx is overkill )

 $arr[$idx]['consignee'] = substr($arr[$idx]['consignee'], 0, -4).'****';

For testing

 $idx = 0;        
 $arr = [[
     'consignee' => 'someguy'
 ]];

 $arr[$idx]['consignee'] = substr($arr[$idx]['consignee'], 0, -4).'****';

 echo $arr[$idx]['consignee'];

Try it here online

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38