Huymada

CNPJ

Nov 29th, 2019
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Receita Federal - Cadastro Nacional de Pessoa Jurídica
  4.  * @package Harverst
  5.  * @version 2019-11-29
  6.  */
  7.  
  8.     # Complementa o número do documento com ZERO à esquerda
  9.         function cnpj_completar($input) {
  10.             return (string) str_pad($input, 12, '0', STR_PAD_LEFT);
  11.         }
  12.  
  13.     # Formatação com máscara de entrada
  14.         function cnpj_formatar($input, $output = '##.###.###/####-##') {
  15.             for($i = 0, $l = strlen($input); $i < $l; ++$i) {
  16.                 $output[strpos($output, '#')] = $input[$i];
  17.             }
  18.             return $output;
  19.         }
  20.  
  21.     # Obtém os digitos verificadores
  22.         function cnpj_verificador($input) {
  23.             $first = (
  24.                 $input[11] * 9 + $input[10] * 8 +
  25.                 $input[9] * 7 + $input[8] * 6 +
  26.                 $input[7] * 5 + $input[6] * 4 +
  27.                 $input[5] * 3 + $input[4] * 2 +
  28.                 $input[3] * 9 + $input[2] * 8 +
  29.                 $input[1] * 7 + $input[0] * 6
  30.             ) % 11;
  31.  
  32.             if($first == 10) $first = 0;
  33.  
  34.             $second = (
  35.                 $first * 9 + $input[11] * 8 +
  36.                 $input[10] * 7 + $input[9] * 6 +
  37.                 $input[8] * 5 + $input[7] * 4 +
  38.                 $input[6] * 3 + $input[5] * 2 +
  39.                 $input[4] * 9 + $input[3] * 8 +
  40.                 $input[2] * 7 + $input[1] * 6 +
  41.                 $input[0] * 5
  42.             ) % 11;
  43.  
  44.             if($second == 10) $second = 0;
  45.  
  46.             $controller = $first * 10 + $second;
  47.                 if($controller === 0) $controller = '00';
  48.                 if($controller > 0 && $controller < 10) $controller = '0' . $controller;
  49.                     return $controller;
  50.         }
Advertisement
Add Comment
Please, Sign In to add comment