3v4l.org

run code in 300+ 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 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 16
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_FCALL                                               'sprintf'
          8        SEND_VAL                                                 'O+cpf+%5B%25s%5D+%C3%A9+%25s%21%0A'
          9        SEND_VAR                                                 !1
         10        INIT_STATIC_METHOD_CALL                                  'CPF', 'validar'
         11        SEND_VAR                                                 !2
         12        DO_FCALL                                      0  $7      
         13      > JMPZ                                                     $7, ->16
         14    >   QM_ASSIGN                                        ~8      'v%C3%A1lido'
         15      > JMP                                                      ->17
         16    >   QM_ASSIGN                                        ~8      'inv%C3%A1lido'
         17    >   SEND_VAL                                                 ~8
         18        DO_ICALL                                         $9      
         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:  55
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        INIT_FCALL                                               'preg_replace'
         45        SEND_VAL                                                 '%23%5Cd%7B9%7D%28%5Cd%7B2%7D%29%24%23'
         46        SEND_VAL                                                 '%241'
         47        SEND_VAR                                                 !0
         48        DO_ICALL                                         $42     
         49        CONCAT                                           ~43     !7, !8
         50        IS_EQUAL                                         ~44     $42, ~43
         51        VERIFY_RETURN_TYPE                                       ~44
         52      > RETURN                                                   ~44
   33    53*       VERIFY_RETURN_TYPE                                       
         54*     > 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:  18
compiled vars:  !0 = $cpf
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   43     1        INIT_FCALL                                               'preg_replace'
          2        SEND_VAL                                                 '%23%5CD%23'
          3        SEND_VAL                                                 ''
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $1      
          6        CAST                                          6  ~2      $1
          7        ASSIGN                                                   !0, ~2
   44     8        INIT_FCALL                                               'preg_replace'
          9        SEND_VAL                                                 '%23%28%5Cd%29%5C1%7B10%7D%23'
         10        SEND_VAL                                                 ''
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $4      
         13        CAST                                          6  ~5      $4
         14        VERIFY_RETURN_TYPE                                       ~5
         15      > RETURN                                                   ~5
   45    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function filtrar

End of class CPF.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.24 ms | 1456 KiB | 16 Q