Guest User

Untitled

a guest
Nov 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. <?php
  2. function validaCPF($cpf) {
  3.  
  4. // Extrai somente os números
  5. $cpf = preg_replace( '/[^0-9]/is', '', $cpf );
  6.  
  7. // Verifica se foi informado todos os digitos corretamente
  8. if (strlen($cpf) != 11) {
  9. return false;
  10. }
  11. // Verifica se foi informada uma sequência de digitos repetidos. Ex: 111.111.111-11
  12. if (preg_match('/(\d)\1{10}/', $cpf)) {
  13. return false;
  14. }
  15. // Faz o calculo para validar o CPF
  16. for ($t = 9; $t < 11; $t++) {
  17. for ($d = 0, $c = 0; $c < $t; $c++) {
  18. $d += $cpf{$c} * (($t + 1) - $c);
  19. }
  20. $d = ((10 * $d) % 11) % 10;
  21. if ($cpf{$c} != $d) {
  22. return false;
  23. }
  24. }
  25. return true;
  26. }
Add Comment
Please, Sign In to add comment