3v4l.org

run code in 500+ 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 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 8
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 8
Branch analysis from position: 28
Branch analysis from position: 8
Branch analysis from position: 14
3 jumps found. (Code = 188) Position 1 = 18, Position 2 = 23, Position 3 = 15
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 8
Branch analysis from position: 28
Branch analysis from position: 8
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 18
filename:       /in/cXmM9
function name:  format
number of ops:  30
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        FRAMELESS_ICALL_3                preg_replace        ~6      '%2F%5CD%2F', ''
          3        OP_DATA                                                      !0
          4        ASSIGN                                                       !2, ~6
    5     5        ASSIGN                                                       !3, ''
    6     6        ASSIGN                                                       !4, 0
          7      > JMP                                                          ->25
    7     8    >   FETCH_DIM_R                                          ~10     !1, !4
          9        ASSIGN                                                       !5, ~10
    8    10        BOOL_NOT                                             ~12     !2
         11      > JMPZ                                                         ~12, ->14
    9    12    >   ASSIGN_OP                                         8          !3, !5
   10    13      > JMP                                                          ->24
   12    14    > > SWITCH_STRING                                                !5, [ 'd':->18, ], ->23
   13    15    >   IS_EQUAL                                                     !5, 'd'
         16      > JMPNZ                                                        ~14, ->18
         17    > > JMP                                                          ->23
   14    18    >   FETCH_DIM_R                                          ~15     !2, 0
         19        ASSIGN_OP                                         8          !3, ~15
   15    20        FRAMELESS_ICALL_2                substr              ~17     !2, 1
         21        ASSIGN                                                       !2, ~17
   16    22      > JMP                                                          ->24
   17    23    >   ASSIGN_OP                                         8          !3, !5
    6    24    >   PRE_INC                                                      !4
         25    >   STRLEN                                               ~21     !1
         26        IS_SMALLER                                                   !4, ~21
         27      > JMPNZ                                                        ~22, ->8
   20    28    > > RETURN                                                       !3
   21    29*     > RETURN                                                       null

End of function format

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
213.84 ms | 2084 KiB | 14 Q