Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. <?php
  2. function validateCNPJ($cnpj)
  3. {
  4. $cnpj = preg_replace('/[^0-9]/', '', (string) $cnpj);
  5.  
  6. if (strlen($cnpj) != 14) {
  7. return false;
  8. }
  9.  
  10. for ($i = 0, $j = 5, $sum = 0; $i < 12; $i++) {
  11. $sum += $cnpj{$i} * $j;
  12. $j = ($j == 2) ? 9 : $j - 1;
  13. }
  14.  
  15. $left = $sum % 11;
  16.  
  17. if ($cnpj{12} != ($left < 2 ? 0 : 11 - $left)) {
  18. return false;
  19. }
  20.  
  21. for ($i = 0, $j = 6, $sum = 0; $i < 13; $i++) {
  22. $sum += $cnpj{$i} * $j;
  23. $j = ($j == 2) ? 9 : $j - 1;
  24. }
  25.  
  26. $left = $sum % 11;
  27.  
  28. return $cnpj{13} == ($left < 2 ? 0 : 11 - $left);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement