3v4l.org

run code in 300+ 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 = 30, Position 2 = 37
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/Kqh3o
function name:  trim_slug
number of ops:  43
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        INIT_FCALL                                               'substr'
         18        FETCH_DIM_R                                      ~15     !3, 'job'
         19        SEND_VAL                                                 ~15
         20        SEND_VAL                                                 0
         21        SEND_VAR                                                 !4
         22        DO_ICALL                                         $16     
         23        ASSIGN                                                   !5, $16
   19    24        INIT_FCALL                                               'strrpos'
         25        SEND_VAR                                                 !5
         26        SEND_VAL                                                 '-'
         27        DO_ICALL                                         $18     
         28        ASSIGN                                           ~19     !6, $18
         29      > JMPZ                                                     ~19, ->37
   20    30    >   INIT_FCALL                                               'substr'
         31        FETCH_DIM_R                                      ~20     !3, 'job'
         32        SEND_VAL                                                 ~20
         33        SEND_VAL                                                 0
         34        SEND_VAR                                                 !6
         35        DO_ICALL                                         $21     
         36        ASSIGN                                                   !5, $21
   23    37    >   FETCH_DIM_R                                      ~23     !3, 'loc'
         38        CONCAT                                           ~24     !5, ~23
         39        VERIFY_RETURN_TYPE                                       ~24
         40      > RETURN                                                   ~24
   24    41*       VERIFY_RETURN_TYPE                                       
         42*     > RETURN                                                   null

End of function trim_slug

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.29 ms | 1020 KiB | 17 Q