Created
October 21, 2014 12:46
-
-
Save bytefade/1c7cf76f05bdc1460266 to your computer and use it in GitHub Desktop.
Mascára em php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
function mask($val, $mask) | |
{ | |
$maskared = ''; | |
$k = 0; | |
for($i = 0; $i<=strlen($mask)-1; $i++) | |
{ | |
if($mask[$i] == '#') | |
{ | |
if(isset($val[$k])) | |
$maskared .= $val[$k++]; | |
} | |
else | |
{ | |
if(isset($mask[$i])) | |
$maskared .= $mask[$i]; | |
} | |
} | |
return $maskared; | |
} | |
?> | |
//Exemplo de uso | |
<? | |
$cnpj = "11222333000199"; | |
$cpf = "00100200300"; | |
$cep = "08665110"; | |
$data = "10102010"; | |
echo mask($cnpj,'##.###.###/####-##'); | |
echo mask($cpf,'###.###.###-##'); | |
echo mask($cep,'#####-###'); | |
echo mask($data,'##/##/####'); | |
?> | |
<? | |
$data = "10102010"; | |
echo mask($data,'##/##/####'); | |
echo mask($data,'[##][##][####]'); | |
echo mask($data,'(##)(##)(####)'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment