I have a field that can contain an EIN or SSN but I want to mask it when I display them in the view. Here’s the code I used to do that. Basically I just count the number of dashes. If there is only one it’s an EIN and if there are two it’s an SSN. I select the correct mask and then append the last four of the string.
if(substr_count($item->ein,"-")==1 ? $mask="XX-XXX" : $mask= "XXX-XX-");
echo $mask . substr($item->ein, -4)