3v4l.org

run code in 500+ PHP versions simultaneously
<?php /** * Removes the diacritical marks from a string. * * Diacritical marks: {@link https://unicode-table.com/blocks/combining-diacritical-marks/} * * @param string $string The string from which to strip the diacritical marks. * @return string Stripped string. */ function stripDiacriticalMarks(string $string): string { return preg_replace('/[\x{0300}-\x{036f}]/u', '', \Normalizer::normalize($string , \Normalizer::FORM_KD)); } /** * Checks if the string $haystack is like $needle, $needle can contain '%' and '_' * characters which will behave as if used in a SQL LIKE condition. Character escaping * is supported with '\'. * * @param string $haystack The string to check if it is like $needle * @param string $needle The string used to check if $haystack is like it * @param bool $ai Whether to check likeness in an accent-insensitive manner * @param bool $ci Whether to check likeness in a case-insensitive manner * @return bool True if $haystack is like $needle, otherwise, false */ function like(string $haystack, string $needle, bool $ai = true, bool $ci = true): bool { if ($ai) { $haystack = stripDiacriticalMarks($haystack); $needle = stripDiacriticalMarks($needle); } $needle = preg_quote($needle, '/'); $tokens = []; $needleLength = strlen($needle); for ($i = 0; $i < $needleLength;) { if ($needle[$i] === '\\') { $i += 2; if ($i < $needleLength) { if ($needle[$i] === '\\') { $tokens[] = '\\\\'; $i += 2; } else { $tokens[] = $needle[$i]; ++$i; } } else { $tokens[] = '\\\\'; } } else { switch ($needle[$i]) { case '_': $tokens[] = '.'; break; case '%': $tokens[] = '.*'; break; default: $tokens[] = $needle[$i]; break; } ++$i; } } return preg_match('/^' . implode($tokens) . '$/u' . ($ci ? 'i' : ''), $haystack) === 1; } /** * Escapes a string in a way that `UString::like` will match it as-is, thus '%' and '_' * would match a literal '%' and '_' respectively (and not behave as in a SQL LIKE * condition). * * @param string $str The string to escape. * @return string The escaped string. */ function escapeLike(string $str): string { return strtr($str, ['\\' => '\\\\', '%' => '\%', '_' => '\_']); } var_dump(like('Hello 🙃', 'Hello _')); var_dump(like('Hello 🙃', '_e%o__')); var_dump(like('asdfas \\🙃H\\\\%🙃É\\l\\_🙃\\l\\o asdfasf', '%' . escapeLike('\\🙃h\\\\%🙃e\\l\\_🙃\\l\\o') . '%'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O9LX0
function name:  (null)
number of ops:  27
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   84     0  E >   INIT_FCALL                                                   'var_dump'
          1        INIT_FCALL                                                   'like'
          2        SEND_VAL                                                     'Hello+%F0%9F%99%83'
          3        SEND_VAL                                                     'Hello+_'
          4        DO_FCALL                                          0  $0      
          5        SEND_VAR                                                     $0
          6        DO_ICALL                                                     
   85     7        INIT_FCALL                                                   'var_dump'
          8        INIT_FCALL                                                   'like'
          9        SEND_VAL                                                     'Hello+%F0%9F%99%83'
         10        SEND_VAL                                                     '_e%25o__'
         11        DO_FCALL                                          0  $2      
         12        SEND_VAR                                                     $2
         13        DO_ICALL                                                     
   86    14        INIT_FCALL                                                   'var_dump'
         15        INIT_FCALL                                                   'like'
         16        SEND_VAL                                                     'asdfas+%5C%F0%9F%99%83H%5C%5C%25%F0%9F%99%83%C3%89%5Cl%5C_%F0%9F%99%83%5Cl%5Co+asdfasf'
         17        INIT_FCALL                                                   'escapelike'
         18        SEND_VAL                                                     '%5C%F0%9F%99%83h%5C%5C%25%F0%9F%99%83e%5Cl%5C_%F0%9F%99%83%5Cl%5Co'
         19        DO_FCALL                                          0  $4      
         20        CONCAT                                               ~5      '%25', $4
         21        CONCAT                                               ~6      ~5, '%25'
         22        SEND_VAL                                                     ~6
         23        DO_FCALL                                          0  $7      
         24        SEND_VAR                                                     $7
         25        DO_ICALL                                                     
         26      > RETURN                                                       1

Function stripdiacriticalmarks:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O9LX0
function name:  stripDiacriticalMarks
number of ops:  12
compiled vars:  !0 = $string
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
   12     1        INIT_STATIC_METHOD_CALL                                      'Normalizer', 'normalize'
          2        SEND_VAR_EX                                                  !0
          3        FETCH_CLASS_CONSTANT                                 ~1      'Normalizer', 'FORM_KD'
          4        SEND_VAL_EX                                                  ~1
          5        DO_FCALL                                          0  $2      
          6        FRAMELESS_ICALL_3                preg_replace        ~3      '%2F%5B%5Cx%7B0300%7D-%5Cx%7B036f%7D%5D%2Fu', ''
          7        OP_DATA                                                      $2
          8        VERIFY_RETURN_TYPE                                           ~3
          9      > RETURN                                                       ~3
   13    10*       VERIFY_RETURN_TYPE                                           
         11*     > RETURN                                                       null

End of function stripdiacriticalmarks

Function like:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 23
Branch analysis from position: 65
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 71
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 71
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 44
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 41
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 36
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
Branch analysis from position: 44
4 jumps found. (Code = 188) Position 1 = 51, Position 2 = 54, Position 3 = 57, Position 4 = 46
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 23
Branch analysis from position: 65
Branch analysis from position: 23
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 51
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 54
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
Branch analysis from position: 54
Branch analysis from position: 51
Branch analysis from position: 13
filename:       /in/O9LX0
function name:  like
number of ops:  79
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $ai, !3 = $ci, !4 = $tokens, !5 = $needleLength, !6 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      <true>
          3        RECV_INIT                                            !3      <true>
   28     4      > JMPZ                                                         !2, ->13
   29     5    >   INIT_FCALL                                                   'stripdiacriticalmarks'
          6        SEND_VAR                                                     !0
          7        DO_FCALL                                          0  $7      
          8        ASSIGN                                                       !0, $7
   30     9        INIT_FCALL                                                   'stripdiacriticalmarks'
         10        SEND_VAR                                                     !1
         11        DO_FCALL                                          0  $9      
         12        ASSIGN                                                       !1, $9
   33    13    >   INIT_FCALL                                                   'preg_quote'
         14        SEND_VAR                                                     !1
         15        SEND_VAL                                                     '%2F'
         16        DO_ICALL                                             $11     
         17        ASSIGN                                                       !1, $11
   35    18        ASSIGN                                                       !4, <array>
   37    19        STRLEN                                               ~14     !1
         20        ASSIGN                                                       !5, ~14
   38    21        ASSIGN                                                       !6, 0
         22      > JMP                                                          ->63
   39    23    >   FETCH_DIM_R                                          ~17     !1, !6
         24        IS_IDENTICAL                                                 ~17, '%5C'
         25      > JMPZ                                                         ~18, ->44
   40    26    >   ASSIGN_OP                                         1          !6, 2
   41    27        IS_SMALLER                                                   !6, !5
         28      > JMPZ                                                         ~20, ->41
   42    29    >   FETCH_DIM_R                                          ~21     !1, !6
         30        IS_IDENTICAL                                                 ~21, '%5C'
         31      > JMPZ                                                         ~22, ->36
   43    32    >   ASSIGN_DIM                                                   !4
         33        OP_DATA                                                      '%5C%5C'
   44    34        ASSIGN_OP                                         1          !6, 2
   42    35      > JMP                                                          ->40
   46    36    >   FETCH_DIM_R                                          ~26     !1, !6
         37        ASSIGN_DIM                                                   !4
         38        OP_DATA                                                      ~26
   47    39        PRE_INC                                                      !6
   41    40    > > JMP                                                          ->43
   50    41    >   ASSIGN_DIM                                                   !4
         42        OP_DATA                                                      '%5C%5C'
   39    43    > > JMP                                                          ->63
   53    44    >   FETCH_DIM_R                                          ~29     !1, !6
         45      > SWITCH_STRING                                                ~29, [ '_':->51, '%25':->54, ], ->57
   54    46    >   CASE                                                         ~29, '_'
         47      > JMPNZ                                                        ~30, ->51
   57    48    >   CASE                                                         ~29, '%25'
         49      > JMPNZ                                                        ~30, ->54
         50    > > JMP                                                          ->57
   55    51    >   ASSIGN_DIM                                                   !4
         52        OP_DATA                                                      '.'
   56    53      > JMP                                                          ->61
   58    54    >   ASSIGN_DIM                                                   !4
         55        OP_DATA                                                      '.%2A'
   59    56      > JMP                                                          ->61
   61    57    >   FETCH_DIM_R                                          ~34     !1, !6
         58        ASSIGN_DIM                                                   !4
         59        OP_DATA                                                      ~34
   62    60      > JMP                                                          ->61
         61    >   FREE                                                         ~29
   64    62        PRE_INC                                                      !6
   38    63    >   IS_SMALLER                                                   !6, !5
         64      > JMPNZ                                                        ~36, ->23
   68    65    >   FRAMELESS_ICALL_1                implode             ~37     !4
         66        CONCAT                                               ~38     '%2F%5E', ~37
         67        CONCAT                                               ~39     ~38, '%24%2Fu'
         68      > JMPZ                                                         !3, ->71
         69    >   QM_ASSIGN                                            ~40     'i'
         70      > JMP                                                          ->72
         71    >   QM_ASSIGN                                            ~40     ''
         72    >   CONCAT                                               ~41     ~39, ~40
         73        FRAMELESS_ICALL_2                preg_match          ~42     ~41, !0
         74        IS_IDENTICAL                                         ~43     ~42, 1
         75        VERIFY_RETURN_TYPE                                           ~43
         76      > RETURN                                                       ~43
   69    77*       VERIFY_RETURN_TYPE                                           
         78*     > RETURN                                                       null

End of function like

Function escapelike:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O9LX0
function name:  escapeLike
number of ops:  6
compiled vars:  !0 = $str
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   79     0  E >   RECV                                                 !0      
   81     1        FRAMELESS_ICALL_2                strtr               ~1      !0, <array>
          2        VERIFY_RETURN_TYPE                                           ~1
          3      > RETURN                                                       ~1
   82     4*       VERIFY_RETURN_TYPE                                           
          5*     > RETURN                                                       null

End of function escapelike

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
169.75 ms | 3330 KiB | 21 Q