3v4l.org

run code in 500+ PHP versions simultaneously
<?php function trim_slug(string $slug, int $maxlen = 150): string { // check if trimming is required: if(strlen($slug) <= $maxlen) { return $slug; } $pattern = '/^(?<job>.+)(?<loc>-in-\d{5}-.*-.*-\d*)$/'; // $match will have 'job' and 'loc' named keys with the matched values preg_match($pattern, $slug, $match); // raw cut of job name to maximum length: $max_job_chars = $maxlen - strlen($match['loc']); $job_name = substr($match['job'], 0, $max_job_chars); // tidy up to last delimiter, if exists, instead of mincing words: if($last_delim = strrpos($job_name, '-')) { $job_name = substr($match['job'], 0, $last_delim); } return $job_name . $match['loc']; } $string = '/job/hello-this-is-my-job-posting-for-a-daycare-im-looking-for-in-91770-rosemead-california-12345'; echo trim_slug($string, 80);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kqh3o
function name:  (null)
number of ops:  7
compiled vars:  !0 = $string
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                       !0, '%2Fjob%2Fhello-this-is-my-job-posting-for-a-daycare-im-looking-for-in-91770-rosemead-california-12345'
   27     1        INIT_FCALL                                                   'trim_slug'
          2        SEND_VAR                                                     !0
          3        SEND_VAL                                                     80
          4        DO_FCALL                                          0  $2      
          5        ECHO                                                         $2
          6      > RETURN                                                       1

Function trim_slug:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/Kqh3o
function name:  trim_slug
number of ops:  37
compiled vars:  !0 = $slug, !1 = $maxlen, !2 = $pattern, !3 = $match, !4 = $max_job_chars, !5 = $job_name, !6 = $last_delim
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      150
    6     2        STRLEN                                               ~7      !0
          3        IS_SMALLER_OR_EQUAL                                          ~7, !1
          4      > JMPZ                                                         ~8, ->7
    7     5    >   VERIFY_RETURN_TYPE                                           !0
          6      > RETURN                                                       !0
   10     7    >   ASSIGN                                                       !2, '%2F%5E%28%3F%3Cjob%3E.%2B%29%28%3F%3Cloc%3E-in-%5Cd%7B5%7D-.%2A-.%2A-%5Cd%2A%29%24%2F'
   12     8        INIT_FCALL                                                   'preg_match'
          9        SEND_VAR                                                     !2
         10        SEND_VAR                                                     !0
         11        SEND_REF                                                     !3
         12        DO_ICALL                                                     
   15    13        FETCH_DIM_R                                          ~11     !3, 'loc'
         14        STRLEN                                               ~12     ~11
         15        SUB                                                  ~13     !1, ~12
         16        ASSIGN                                                       !4, ~13
   16    17        FETCH_DIM_R                                          ~15     !3, 'job'
         18        FRAMELESS_ICALL_3                substr              ~16     ~15, 0
         19        OP_DATA                                                      !4
         20        ASSIGN                                                       !5, ~16
   19    21        INIT_FCALL                                                   'strrpos'
         22        SEND_VAR                                                     !5
         23        SEND_VAL                                                     '-'
         24        DO_ICALL                                             $18     
         25        ASSIGN                                               ~19     !6, $18
         26      > JMPZ                                                         ~19, ->31
   20    27    >   FETCH_DIM_R                                          ~20     !3, 'job'
         28        FRAMELESS_ICALL_3                substr              ~21     ~20, 0
         29        OP_DATA                                                      !6
         30        ASSIGN                                                       !5, ~21
   23    31    >   FETCH_DIM_R                                          ~23     !3, 'loc'
         32        CONCAT                                               ~24     !5, ~23
         33        VERIFY_RETURN_TYPE                                           ~24
         34      > RETURN                                                       ~24
   24    35*       VERIFY_RETURN_TYPE                                           
         36*     > RETURN                                                       null

End of function trim_slug

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
165.08 ms | 1728 KiB | 16 Q