3v4l.org

run code in 300+ PHP versions simultaneously
<?php function format($src, $format = '+d (ddd) ddd-dd-dd') { $digits = preg_replace('/\D/', '', $src); // Оставляем только цифры $res = ''; for($i = 0; $i < strlen($format); $i++) { $letter = $format[$i]; if (!$digits) { // Не хватило цифр $res .= $letter; continue; } switch ($letter) { case 'd': // Цифра $res .= $digits[0]; $digits = substr($digits, 1); break; default: $res .= $letter; } } return $res; } $data = [ '+7(999)999-99-99', '79998887766', '799988877', // недостаточно цифр для формата ]; foreach ($data as $phoneSrc) { echo format($phoneSrc)."\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 9
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/cXmM9
function name:  (null)
number of ops:  11
compiled vars:  !0 = $data, !1 = $phoneSrc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   ASSIGN                                                   !0, <array>
   29     1      > FE_RESET_R                                       $3      !0, ->9
          2    > > FE_FETCH_R                                               $3, !1, ->9
   30     3    >   INIT_FCALL                                               'format'
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        CONCAT                                           ~5      $4, '%0A'
          7        ECHO                                                     ~5
   29     8      > JMP                                                      ->2
          9    >   FE_FREE                                                  $3
   31    10      > RETURN                                                   1

Function format:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 11
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 11
Branch analysis from position: 34
Branch analysis from position: 11
Branch analysis from position: 17
3 jumps found. (Code = 188) Position 1 = 21, Position 2 = 29, Position 3 = 18
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 11
Branch analysis from position: 34
Branch analysis from position: 11
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 21
filename:       /in/cXmM9
function name:  format
number of ops:  36
compiled vars:  !0 = $src, !1 = $format, !2 = $digits, !3 = $res, !4 = $i, !5 = $letter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '%2Bd+%28ddd%29+ddd-dd-dd'
    4     2        INIT_FCALL                                               'preg_replace'
          3        SEND_VAL                                                 '%2F%5CD%2F'
          4        SEND_VAL                                                 ''
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $6      
          7        ASSIGN                                                   !2, $6
    5     8        ASSIGN                                                   !3, ''
    6     9        ASSIGN                                                   !4, 0
         10      > JMP                                                      ->31
    7    11    >   FETCH_DIM_R                                      ~10     !1, !4
         12        ASSIGN                                                   !5, ~10
    8    13        BOOL_NOT                                         ~12     !2
         14      > JMPZ                                                     ~12, ->17
    9    15    >   ASSIGN_OP                                     8          !3, !5
   10    16      > JMP                                                      ->30
   12    17    > > SWITCH_STRING                                            !5, [ 'd':->21, ], ->29
   13    18    >   IS_EQUAL                                                 !5, 'd'
         19      > JMPNZ                                                    ~14, ->21
         20    > > JMP                                                      ->29
   14    21    >   FETCH_DIM_R                                      ~15     !2, 0
         22        ASSIGN_OP                                     8          !3, ~15
   15    23        INIT_FCALL                                               'substr'
         24        SEND_VAR                                                 !2
         25        SEND_VAL                                                 1
         26        DO_ICALL                                         $17     
         27        ASSIGN                                                   !2, $17
   16    28      > JMP                                                      ->30
   17    29    >   ASSIGN_OP                                     8          !3, !5
    6    30    >   PRE_INC                                                  !4
         31    >   STRLEN                                           ~21     !1
         32        IS_SMALLER                                               !4, ~21
         33      > JMPNZ                                                    ~22, ->11
   20    34    > > RETURN                                                   !3
   21    35*     > RETURN                                                   null

End of function format

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.62 ms | 1403 KiB | 18 Q