3v4l.org

run code in 500+ PHP versions simultaneously
<?php class CPF { // Codigo que valida um CPF informado quanto a validade de seus dígitos verificadores. /** * * Verifica se um valor informado é um CPF válido * @author Marcos Regis <marcos@marcosregis.com> * @param string $cpf * @return bool */ public static function validar(string $cpf): bool { if (strlen($cpf) != 11) { return false; } $elementos = (array)str_split($cpf); $elementos[10] = 0; // Reduz uma comparação no calculo de $somaB $somaA = 0; $somaB = 0; foreach ($elementos as $indice => $elemento) { $multiplicador = count($elementos) - $indice; $somaA += (int)$elemento * (int)($multiplicador > 2 ? $multiplicador - 1 : 0); $somaB += (int)$elemento * (int)$multiplicador; } $moduloA = (($somaA * 10) % 11) % 10; $moduloB = (($somaB * 10) % 11) % 10; return preg_replace('#\d{9}(\d{2})$#', '$1', $cpf) == $moduloA . $moduloB; } /** * Filtra um CPF informado em <var>$cpf</var> removendo mascaras e valores como 111.111.111-11 que são * falsos positivos * @param string $cpf * @return string */ public static function filtrar(string $cpf): string { $cpf = (string)preg_replace('#\D#', '', $cpf); return (string)preg_replace('#(\d)\1{10}#', '', $cpf); } } $cpfs = [ '529.982.247-25', // válido, com mascara '123.456.789-09', // válido, regra do resto 0 ou 1 '834.115.113-89', // válido, com mascara '50365387223', // válido, sem mascara '112413460-38', // válido, com apenas parte da máscara '242.629.250-92', // inválido, com máscara '88888888888', // inválido, falso positivo sem máscaa=ra '123.456.789', // inválido, incompleto com máscara '123456789', // inválido, incompleto sem máscara '', // inválido, vazio ]; foreach ($cpfs as $cpf) { $cpfFiltrado = CPF::filtrar($cpf); echo sprintf('O cpf [%s] é %s!' . PHP_EOL, $cpf, (CPF::validar($cpfFiltrado) ? 'válido' : 'inválido')); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 21
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 21
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/g1RdF
function name:  (null)
number of ops:  23
compiled vars:  !0 = $cpfs, !1 = $cpf, !2 = $cpfFiltrado
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   48     0  E >   ASSIGN                                                       !0, <array>
   61     1      > FE_RESET_R                                           $4      !0, ->21
          2    > > FE_FETCH_R                                                   $4, !1, ->21
   62     3    >   INIT_STATIC_METHOD_CALL                                      'CPF', 'filtrar'
          4        SEND_VAR                                                     !1
          5        DO_FCALL                                          0  $5      
          6        ASSIGN                                                       !2, $5
   63     7        INIT_STATIC_METHOD_CALL                                      'CPF', 'validar'
          8        SEND_VAR                                                     !2
          9        DO_FCALL                                          0  $7      
         10      > JMPZ                                                         $7, ->13
         11    >   QM_ASSIGN                                            ~8      'v%C3%A1lido'
         12      > JMP                                                          ->14
         13    >   QM_ASSIGN                                            ~8      'inv%C3%A1lido'
         14    >   ROPE_INIT                                         5  ~10     'O+cpf+%5B'
         15        ROPE_ADD                                          1  ~10     ~10, !1
         16        ROPE_ADD                                          2  ~10     ~10, '%5D+%C3%A9+'
         17        ROPE_ADD                                          3  ~10     ~10, ~8
         18        ROPE_END                                          4  ~9      ~10, '%21%0A'
         19        ECHO                                                         ~9
   61    20      > JMP                                                          ->2
         21    >   FE_FREE                                                      $4
   64    22      > RETURN                                                       1

Class CPF:
Function validar:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 35
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 35
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 26
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
filename:       /in/g1RdF
function name:  validar
number of ops:  52
compiled vars:  !0 = $cpf, !1 = $elementos, !2 = $somaA, !3 = $somaB, !4 = $elemento, !5 = $indice, !6 = $multiplicador, !7 = $moduloA, !8 = $moduloB
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
   16     1        STRLEN                                               ~9      !0
          2        IS_NOT_EQUAL                                                 ~9, 11
          3      > JMPZ                                                         ~10, ->5
   17     4    > > RETURN                                                       <false>
   19     5    >   INIT_FCALL                                                   'str_split'
          6        SEND_VAR                                                     !0
          7        DO_ICALL                                             $11     
          8        CAST                                              7  ~12     $11
          9        ASSIGN                                                       !1, ~12
   20    10        ASSIGN_DIM                                                   !1, 10
         11        OP_DATA                                                      0
   21    12        ASSIGN                                                       !2, 0
   22    13        ASSIGN                                                       !3, 0
   23    14      > FE_RESET_R                                           $17     !1, ->35
         15    > > FE_FETCH_R                                           ~18     $17, !4, ->35
         16    >   ASSIGN                                                       !5, ~18
   24    17        COUNT                                                ~20     !1
         18        SUB                                                  ~21     ~20, !5
         19        ASSIGN                                                       !6, ~21
   25    20        CAST                                              4  ~23     !4
         21        IS_SMALLER                                                   2, !6
         22      > JMPZ                                                         ~24, ->26
         23    >   SUB                                                  ~25     !6, 1
         24        QM_ASSIGN                                            ~26     ~25
         25      > JMP                                                          ->27
         26    >   QM_ASSIGN                                            ~26     0
         27    >   CAST                                              4  ~27     ~26
         28        MUL                                                  ~28     ~23, ~27
         29        ASSIGN_OP                                         1          !2, ~28
   26    30        CAST                                              4  ~30     !4
         31        CAST                                              4  ~31     !6
         32        MUL                                                  ~32     ~30, ~31
         33        ASSIGN_OP                                         1          !3, ~32
   23    34      > JMP                                                          ->15
         35    >   FE_FREE                                                      $17
   29    36        MUL                                                  ~34     !2, 10
         37        MOD                                                  ~35     ~34, 11
         38        MOD                                                  ~36     ~35, 10
         39        ASSIGN                                                       !7, ~36
   30    40        MUL                                                  ~38     !3, 10
         41        MOD                                                  ~39     ~38, 11
         42        MOD                                                  ~40     ~39, 10
         43        ASSIGN                                                       !8, ~40
   32    44        FRAMELESS_ICALL_3                preg_replace        ~42     '%23%5Cd%7B9%7D%28%5Cd%7B2%7D%29%24%23', '%241'
         45        OP_DATA                                                      !0
         46        CONCAT                                               ~43     !7, !8
         47        IS_EQUAL                                             ~44     ~42, ~43
         48        VERIFY_RETURN_TYPE                                           ~44
         49      > RETURN                                                       ~44
   33    50*       VERIFY_RETURN_TYPE                                           
         51*     > RETURN                                                       null

End of function validar

Function filtrar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/g1RdF
function name:  filtrar
number of ops:  12
compiled vars:  !0 = $cpf
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   41     0  E >   RECV                                                 !0      
   43     1        FRAMELESS_ICALL_3                preg_replace        ~1      '%23%5CD%23', ''
          2        OP_DATA                                                      !0
          3        CAST                                              6  ~2      ~1
          4        ASSIGN                                                       !0, ~2
   44     5        FRAMELESS_ICALL_3                preg_replace        ~4      '%23%28%5Cd%29%5C1%7B10%7D%23', ''
          6        OP_DATA                                                      !0
          7        CAST                                              6  ~5      ~4
          8        VERIFY_RETURN_TYPE                                           ~5
          9      > RETURN                                                       ~5
   45    10*       VERIFY_RETURN_TYPE                                           
         11*     > RETURN                                                       null

End of function filtrar

End of class CPF.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
195.25 ms | 1568 KiB | 14 Q