How to hide characters from a Cpf using substr_replace() in table column?

Asked

Viewed 148 times

3

I was able to hide the characters of an email in an employee listing table using substr_replace() leaving as an example c*****[email protected]. Now I don’t know how to do it in the field cpf.

Here is the example in the field email:

echo '<td title="'.substr_replace($exibir_colunas['email'], '******', 1, strpos($exibir_colunas['email'], '@') - 2).'">'.substr_replace($exibir_colunas['email'], '*****', 1, strpos($exibir_colunas['email'], '@') - 2).'</td>';

I’d like to do the same in the country cpf leaving 012.***.***.90 using substr_replace().

echo '<td title="'.$exibir_colunas['cpf'].'">'.$exibir_colunas['cpf'].'</td>';

1 answer

5


If you are absolutely sure that there will be 14 characters (11 numbers plus 3 points), then just apply in the second parameter 4, which represents 3 digits plus the dot, and in the third parameter put the negative number 3 (1 point and 2 digits )

<?php

$var = '100.222.333.44';

echo substr_replace($var, '***.***', 4, -3);

If you merge Cpfs and Cnpjs then you can check the amount of characters and adjust, example:

<?php

function mascara_doc($doc)
{
    if (strlen($doc) === 18) {
         return substr_replace($doc, '***.***/****', 3, -3);
    }

    return substr_replace($doc, '***.***', 4, -3);
}

var_dump(mascara_doc('100.222.333.44'));
var_dump(mascara_doc('12.345.678/1000-55'));

Online example: https://ideone.com/OuyErt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.