3v4l.org

run code in 300+ PHP versions simultaneously
<?php function slugify($str, $options = array()) { // Make sure string is in UTF-8 and strip invalid UTF-8 characters $str = mb_convert_encoding((string)$str, 'UTF-8', mb_list_encodings()); $defaults = array( 'delimiter' => '-', 'limit' => null, 'lowercase' => true, 'replacements' => array(), 'transliterate' => false, ); // Merge options $options = array_merge($defaults, $options); $char_map = array( // Latin 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y', // Latin symbols '©' => '(c)', // Greek 'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8', 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W', 'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I', 'Ϋ' => 'Y', 'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p', 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w', 'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's', 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i', // Turkish 'Ş' => 'S', 'İ' => 'I', 'Ç' => 'C', 'Ü' => 'U', 'Ö' => 'O', 'Ğ' => 'G', 'ş' => 's', 'ı' => 'i', 'ç' => 'c', 'ü' => 'u', 'ö' => 'o', 'ğ' => 'g', // Russian 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu', 'я' => 'ya', // Ukrainian 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g', // Czech 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U', 'Ž' => 'Z', 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u', 'ž' => 'z', // Polish 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'o', 'Ś' => 'S', 'Ź' => 'Z', 'Ż' => 'Z', 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z', 'ż' => 'z', // Latvian 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i', 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'u', 'Ž' => 'Z', 'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n', 'š' => 's', 'ū' => 'u', 'ž' => 'z' ); // Make custom replacements $str = preg_replace(array_keys($options['replacements']), $options['replacements'], $str); // Transliterate characters to ASCII if ($options['transliterate']) { $str = str_replace(array_keys($char_map), $char_map, $str); } // Replace non-alphanumeric characters with our delimiter $str = preg_replace('/[^\p{L}\p{Nd}]+/u', $options['delimiter'], $str); // Remove duplicate delimiters $str = preg_replace('/(' . preg_quote($options['delimiter'], '/') . '){2,}/', '$1', $str); // Truncate slug to max. characters $str = mb_substr($str, 0, ($options['limit'] ? $options['limit'] : mb_strlen($str, 'UTF-8')), 'UTF-8'); // Remove delimiter from ends $str = trim($str, $options['delimiter']); return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str; } echo slugify("Straight Soft (French)", array('delimiter' => '', 'replacements' => array('/_/' => '')));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nUSlI
function name:  (null)
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  108     0  E >   INIT_FCALL                                               'slugify'
          1        SEND_VAL                                                 'Straight+Soft+%28French%29'
          2        SEND_VAL                                                 <array>
          3        DO_FCALL                                      0  $0      
          4        ECHO                                                     $0
          5      > RETURN                                                   1

Function slugify:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 40
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 68
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 91
Branch analysis from position: 85
1 jumps found. (Code = 42) Position 1 = 92
Branch analysis from position: 92
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 91
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
2 jumps found. (Code = 43) Position 1 = 85, Position 2 = 91
Branch analysis from position: 85
Branch analysis from position: 91
Branch analysis from position: 40
filename:       /in/nUSlI
function name:  slugify
number of ops:  94
compiled vars:  !0 = $str, !1 = $options, !2 = $defaults, !3 = $char_map
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
    5     2        INIT_FCALL                                               'mb_convert_encoding'
          3        CAST                                          6  ~4      !0
          4        SEND_VAL                                                 ~4
          5        SEND_VAL                                                 'UTF-8'
          6        INIT_FCALL                                               'mb_list_encodings'
          7        DO_ICALL                                         $5      
          8        SEND_VAR                                                 $5
          9        DO_ICALL                                         $6      
         10        ASSIGN                                                   !0, $6
    7    11        ASSIGN                                                   !2, <array>
   16    12        INIT_FCALL                                               'array_merge'
         13        SEND_VAR                                                 !2
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $9      
         16        ASSIGN                                                   !1, $9
   18    17        ASSIGN                                                   !3, <array>
   86    18        INIT_FCALL                                               'preg_replace'
         19        INIT_FCALL                                               'array_keys'
         20        FETCH_DIM_R                                      ~12     !1, 'replacements'
         21        SEND_VAL                                                 ~12
         22        DO_ICALL                                         $13     
         23        SEND_VAR                                                 $13
         24        FETCH_DIM_R                                      ~14     !1, 'replacements'
         25        SEND_VAL                                                 ~14
         26        SEND_VAR                                                 !0
         27        DO_ICALL                                         $15     
         28        ASSIGN                                                   !0, $15
   89    29        FETCH_DIM_R                                      ~17     !1, 'transliterate'
         30      > JMPZ                                                     ~17, ->40
   90    31    >   INIT_FCALL                                               'str_replace'
         32        INIT_FCALL                                               'array_keys'
         33        SEND_VAR                                                 !3
         34        DO_ICALL                                         $18     
         35        SEND_VAR                                                 $18
         36        SEND_VAR                                                 !3
         37        SEND_VAR                                                 !0
         38        DO_ICALL                                         $19     
         39        ASSIGN                                                   !0, $19
   94    40    >   INIT_FCALL                                               'preg_replace'
         41        SEND_VAL                                                 '%2F%5B%5E%5Cp%7BL%7D%5Cp%7BNd%7D%5D%2B%2Fu'
         42        FETCH_DIM_R                                      ~21     !1, 'delimiter'
         43        SEND_VAL                                                 ~21
         44        SEND_VAR                                                 !0
         45        DO_ICALL                                         $22     
         46        ASSIGN                                                   !0, $22
   97    47        INIT_FCALL                                               'preg_replace'
         48        INIT_FCALL                                               'preg_quote'
         49        FETCH_DIM_R                                      ~24     !1, 'delimiter'
         50        SEND_VAL                                                 ~24
         51        SEND_VAL                                                 '%2F'
         52        DO_ICALL                                         $25     
         53        CONCAT                                           ~26     '%2F%28', $25
         54        CONCAT                                           ~27     ~26, '%29%7B2%2C%7D%2F'
         55        SEND_VAL                                                 ~27
         56        SEND_VAL                                                 '%241'
         57        SEND_VAR                                                 !0
         58        DO_ICALL                                         $28     
         59        ASSIGN                                                   !0, $28
  100    60        INIT_FCALL                                               'mb_substr'
         61        SEND_VAR                                                 !0
         62        SEND_VAL                                                 0
         63        FETCH_DIM_R                                      ~30     !1, 'limit'
         64      > JMPZ                                                     ~30, ->68
         65    >   FETCH_DIM_R                                      ~31     !1, 'limit'
         66        QM_ASSIGN                                        ~32     ~31
         67      > JMP                                                      ->73
         68    >   INIT_FCALL                                               'mb_strlen'
         69        SEND_VAR                                                 !0
         70        SEND_VAL                                                 'UTF-8'
         71        DO_ICALL                                         $33     
         72        QM_ASSIGN                                        ~32     $33
         73    >   SEND_VAL                                                 ~32
         74        SEND_VAL                                                 'UTF-8'
         75        DO_ICALL                                         $34     
         76        ASSIGN                                                   !0, $34
  103    77        INIT_FCALL                                               'trim'
         78        SEND_VAR                                                 !0
         79        FETCH_DIM_R                                      ~36     !1, 'delimiter'
         80        SEND_VAL                                                 ~36
         81        DO_ICALL                                         $37     
         82        ASSIGN                                                   !0, $37
  105    83        FETCH_DIM_R                                      ~39     !1, 'lowercase'
         84      > JMPZ                                                     ~39, ->91
         85    >   INIT_FCALL                                               'mb_strtolower'
         86        SEND_VAR                                                 !0
         87        SEND_VAL                                                 'UTF-8'
         88        DO_ICALL                                         $40     
         89        QM_ASSIGN                                        ~41     $40
         90      > JMP                                                      ->92
         91    >   QM_ASSIGN                                        ~41     !0
         92    > > RETURN                                                   ~41
  106    93*     > RETURN                                                   null

End of function slugify

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.73 ms | 1407 KiB | 36 Q