Skip to content

Instantly share code, notes, and snippets.

@renorram
Last active August 30, 2017 14:13
Show Gist options
  • Save renorram/ad848804cb7f3c5cbfed2b90a58dc8be to your computer and use it in GitHub Desktop.
Save renorram/ad848804cb7f3c5cbfed2b90a58dc8be to your computer and use it in GitHub Desktop.
Aplica máscara em string. (en) Apply mask on a string.
<?php
/**
* Aplica uma máscara a string
*
* @param $str
* @param $mask
*
* @return string
*/
function str_mask($str, $mask) {
for ($i = 0; $i < strlen($mask); $i++) {
if ($mask[$i] != '#') {
$str = substr_replace($str, $mask[$i], $i, 0);
}
}
return $str;
}
/** EXAMPLE **/
// cellphone
$value = '85000010001';
print str_mask($value, '(##)#####-####');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment