3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = "Jim & Bob's (amazing!) Photo #2 w/Susan [@Melbourne, Australia].jpg"; echo nl2br($str ."\n"); echo sanitizeFilename($str); function sanitizeFilename($f) { //FROM: http://www.house6.com/blog/?p=83 // a combination of various methods // we don't want to convert html entities, or do any url encoding // we want to retain the "essence" of the original file name, if possible // char replace table found at: // http://www.php.net/manual/en/function.strtr.php#98669 /* Usage example: $str = "Jim & Bob's (amazing!) Photo #2 [@Melbourne, Australia].jpg"; echo sanitizeFilename($str); // Outputs: // jim-and-bobs-amazing-photo-number-2-at-melbourne-australia.jpg */ $replace_chars = array( 'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f' ); $f = strtr($f, $replace_chars); // convert & to "and", @ to "at", and # to "number" $f = preg_replace(array('/[\&]/', '/[\@]/', '/[\#]/' ), array('-and-', '-at-', '-number-'), $f); $f = preg_replace('/[^(\x20-\x7F)]*/','', $f); // removes any special chars we missed $f = str_replace(' ', '-', $f); // convert space to hyphen $f = str_replace('\'', '', $f); // removes apostrophes $f = preg_replace('/[^\w\-\.]+/', '', $f); // remove non-word chars (leaving hyphens and periods) $f = preg_replace('/[\-]+/', '-', $f); // converts groups of hyphens into one $abbrev_words = [ ['word' => 'S', 'abbrev' => 'South'], ['word' => 'W', 'abbrev' => 'West'], ['word' => 'N', 'abbrev' => 'North'], ['word' => 'E', 'abbrev' => 'East'], ]; foreach ($abbrev_words as $row) { $search[] = '/(?<=\s|^)' . preg_quote($row['word'], '/') . '\b/i'; $abbrev[] = $row['abbrev']; } $title = "Heaven's Basement in W Virginia"; echo preg_replace($search, $abbrev, $title); return strtolower($f); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HVdo5
function name:  (null)
number of ops:  11
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, 'Jim+%26+Bob%27s+%28amazing%21%29+Photo+%232+w%2FSusan+%5B%40Melbourne%2C+Australia%5D.jpg'
    5     1        INIT_FCALL                                               'nl2br'
          2        CONCAT                                           ~2      !0, '%0A'
          3        SEND_VAL                                                 ~2
          4        DO_ICALL                                         $3      
          5        ECHO                                                     $3
    6     6        INIT_FCALL_BY_NAME                                       'sanitizeFilename'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $4      
          9        ECHO                                                     $4
   61    10      > RETURN                                                   1

Function sanitizefilename:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 45, Position 2 = 59
Branch analysis from position: 45
2 jumps found. (Code = 78) Position 1 = 46, Position 2 = 59
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
filename:       /in/HVdo5
function name:  sanitizeFilename
number of ops:  72
compiled vars:  !0 = $f, !1 = $replace_chars, !2 = $abbrev_words, !3 = $row, !4 = $search, !5 = $abbrev, !6 = $title
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
   27     1        ASSIGN                                                   !1, <array>
   36     2        INIT_FCALL                                               'strtr'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $8      
          6        ASSIGN                                                   !0, $8
   38     7        INIT_FCALL                                               'preg_replace'
          8        SEND_VAL                                                 <array>
          9        SEND_VAL                                                 <array>
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $10     
         12        ASSIGN                                                   !0, $10
   39    13        INIT_FCALL                                               'preg_replace'
         14        SEND_VAL                                                 '%2F%5B%5E%28%5Cx20-%5Cx7F%29%5D%2A%2F'
         15        SEND_VAL                                                 ''
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                         $12     
         18        ASSIGN                                                   !0, $12
   40    19        INIT_FCALL                                               'str_replace'
         20        SEND_VAL                                                 '+'
         21        SEND_VAL                                                 '-'
         22        SEND_VAR                                                 !0
         23        DO_ICALL                                         $14     
         24        ASSIGN                                                   !0, $14
   41    25        INIT_FCALL                                               'str_replace'
         26        SEND_VAL                                                 '%27'
         27        SEND_VAL                                                 ''
         28        SEND_VAR                                                 !0
         29        DO_ICALL                                         $16     
         30        ASSIGN                                                   !0, $16
   42    31        INIT_FCALL                                               'preg_replace'
         32        SEND_VAL                                                 '%2F%5B%5E%5Cw%5C-%5C.%5D%2B%2F'
         33        SEND_VAL                                                 ''
         34        SEND_VAR                                                 !0
         35        DO_ICALL                                         $18     
         36        ASSIGN                                                   !0, $18
   43    37        INIT_FCALL                                               'preg_replace'
         38        SEND_VAL                                                 '%2F%5B%5C-%5D%2B%2F'
         39        SEND_VAL                                                 '-'
         40        SEND_VAR                                                 !0
         41        DO_ICALL                                         $20     
         42        ASSIGN                                                   !0, $20
   46    43        ASSIGN                                                   !2, <array>
   52    44      > FE_RESET_R                                       $23     !2, ->59
         45    > > FE_FETCH_R                                               $23, !3, ->59
   53    46    >   INIT_FCALL                                               'preg_quote'
         47        FETCH_DIM_R                                      ~25     !3, 'word'
         48        SEND_VAL                                                 ~25
         49        SEND_VAL                                                 '%2F'
         50        DO_ICALL                                         $26     
         51        CONCAT                                           ~27     '%2F%28%3F%3C%3D%5Cs%7C%5E%29', $26
         52        CONCAT                                           ~28     ~27, '%5Cb%2Fi'
         53        ASSIGN_DIM                                               !4
         54        OP_DATA                                                  ~28
   54    55        FETCH_DIM_R                                      ~30     !3, 'abbrev'
         56        ASSIGN_DIM                                               !5
         57        OP_DATA                                                  ~30
   52    58      > JMP                                                      ->45
         59    >   FE_FREE                                                  $23
   57    60        ASSIGN                                                   !6, 'Heaven%27s+Basement+in+W+Virginia'
   58    61        INIT_FCALL                                               'preg_replace'
         62        SEND_VAR                                                 !4
         63        SEND_VAR                                                 !5
         64        SEND_VAR                                                 !6
         65        DO_ICALL                                         $32     
         66        ECHO                                                     $32
   60    67        INIT_FCALL                                               'strtolower'
         68        SEND_VAR                                                 !0
         69        DO_ICALL                                         $33     
         70      > RETURN                                                   $33
   61    71*     > RETURN                                                   null

End of function sanitizefilename

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.19 ms | 1451 KiB | 19 Q