3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getFilenameFromUrl($url) { $default = uniqid(); if (!is_string($url) || strpos($url, '/') === false) { return $default; } // Get last part of the url (after the last slash). $parts = explode('/', $url); $filename = array_pop($parts); // Replace special characters with underscores. $filename = preg_replace('/[^a-z0-9\_\-\.]/i', '_', $filename); // Replace multiple underscores with just one. $filename = preg_replace('/__+/i', '_', trim($filename, '_')); // Return the cleaned filename or, if it is empty, a unique id. return $filename ?: $default; } var_dump(getFilenameFromUrl('THIS IS NOT A URL')); var_dump(getFilenameFromUrl('https://example.com/this____or_that______.zip?dlid=asd____a_a_a__b__b__B')); var_dump(getFilenameFromUrl('https://www.some-domain.com/downloads/file.php?file=foobar')); var_dump(getFilenameFromUrl('https://www.some-domain.com/downloads/file.zip?slid=123')); var_dump(getFilenameFromUrl('https://www.some-domain.com/downloads/?file=foobar'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/W0iUJ
function name:  (null)
number of ops:  31
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'getfilenamefromurl'
          2        SEND_VAL                                                 'THIS+IS+NOT+A+URL'
          3        DO_FCALL                                      0  $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
   22     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL                                               'getfilenamefromurl'
          8        SEND_VAL                                                 'https%3A%2F%2Fexample.com%2Fthis____or_that______.zip%3Fdlid%3Dasd____a_a_a__b__b__B'
          9        DO_FCALL                                      0  $2      
         10        SEND_VAR                                                 $2
         11        DO_ICALL                                                 
   23    12        INIT_FCALL                                               'var_dump'
         13        INIT_FCALL                                               'getfilenamefromurl'
         14        SEND_VAL                                                 'https%3A%2F%2Fwww.some-domain.com%2Fdownloads%2Ffile.php%3Ffile%3Dfoobar'
         15        DO_FCALL                                      0  $4      
         16        SEND_VAR                                                 $4
         17        DO_ICALL                                                 
   24    18        INIT_FCALL                                               'var_dump'
         19        INIT_FCALL                                               'getfilenamefromurl'
         20        SEND_VAL                                                 'https%3A%2F%2Fwww.some-domain.com%2Fdownloads%2Ffile.zip%3Fslid%3D123'
         21        DO_FCALL                                      0  $6      
         22        SEND_VAR                                                 $6
         23        DO_ICALL                                                 
   25    24        INIT_FCALL                                               'var_dump'
         25        INIT_FCALL                                               'getfilenamefromurl'
         26        SEND_VAL                                                 'https%3A%2F%2Fwww.some-domain.com%2Fdownloads%2F%3Ffile%3Dfoobar'
         27        DO_FCALL                                      0  $8      
         28        SEND_VAR                                                 $8
         29        DO_ICALL                                                 
         30      > RETURN                                                   1

Function getfilenamefromurl:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/W0iUJ
function name:  getFilenameFromUrl
number of ops:  44
compiled vars:  !0 = $url, !1 = $default, !2 = $parts, !3 = $filename
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'uniqid'
          2        DO_ICALL                                         $4      
          3        ASSIGN                                                   !1, $4
    6     4        TYPE_CHECK                                   64  ~6      !0
          5        BOOL_NOT                                         ~7      ~6
          6      > JMPNZ_EX                                         ~7      ~7, ->13
          7    >   INIT_FCALL                                               'strpos'
          8        SEND_VAR                                                 !0
          9        SEND_VAL                                                 '%2F'
         10        DO_ICALL                                         $8      
         11        TYPE_CHECK                                    4  ~9      $8
         12        BOOL                                             ~7      ~9
         13    > > JMPZ                                                     ~7, ->15
    8    14    > > RETURN                                                   !1
   11    15    >   INIT_FCALL                                               'explode'
         16        SEND_VAL                                                 '%2F'
         17        SEND_VAR                                                 !0
         18        DO_ICALL                                         $10     
         19        ASSIGN                                                   !2, $10
   12    20        INIT_FCALL                                               'array_pop'
         21        SEND_REF                                                 !2
         22        DO_ICALL                                         $12     
         23        ASSIGN                                                   !3, $12
   14    24        INIT_FCALL                                               'preg_replace'
         25        SEND_VAL                                                 '%2F%5B%5Ea-z0-9%5C_%5C-%5C.%5D%2Fi'
         26        SEND_VAL                                                 '_'
         27        SEND_VAR                                                 !3
         28        DO_ICALL                                         $14     
         29        ASSIGN                                                   !3, $14
   16    30        INIT_FCALL                                               'preg_replace'
         31        SEND_VAL                                                 '%2F__%2B%2Fi'
         32        SEND_VAL                                                 '_'
         33        INIT_FCALL                                               'trim'
         34        SEND_VAR                                                 !3
         35        SEND_VAL                                                 '_'
         36        DO_ICALL                                         $16     
         37        SEND_VAR                                                 $16
         38        DO_ICALL                                         $17     
         39        ASSIGN                                                   !3, $17
   18    40        JMP_SET                                          ~19     !3, ->42
         41        QM_ASSIGN                                        ~19     !1
         42      > RETURN                                                   ~19
   19    43*     > RETURN                                                   null

End of function getfilenamefromurl

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.39 ms | 1012 KiB | 25 Q