3v4l.org

run code in 300+ 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:  15
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   12     1        INIT_FCALL                                               'preg_replace'
          2        SEND_VAL                                                 '%2F%5B%5Cx%7B0300%7D-%5Cx%7B036f%7D%5D%2Fu'
          3        SEND_VAL                                                 ''
          4        INIT_STATIC_METHOD_CALL                                  'Normalizer', 'normalize'
          5        SEND_VAR_EX                                              !0
          6        FETCH_CLASS_CONSTANT                             ~1      'Normalizer', 'FORM_KD'
          7        SEND_VAL_EX                                              ~1
          8        DO_FCALL                                      0  $2      
          9        SEND_VAR                                                 $2
         10        DO_ICALL                                         $3      
         11        VERIFY_RETURN_TYPE                                       $3
         12      > RETURN                                                   $3
   13    13*       VERIFY_RETURN_TYPE                                       
         14*     > 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 = 72, Position 2 = 74
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 74
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:  84
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    >   INIT_FCALL                                               'preg_match'
         66        INIT_FCALL                                               'implode'
         67        SEND_VAR                                                 !4
         68        DO_ICALL                                         $37     
         69        CONCAT                                           ~38     '%2F%5E', $37
         70        CONCAT                                           ~39     ~38, '%24%2Fu'
         71      > JMPZ                                                     !3, ->74
         72    >   QM_ASSIGN                                        ~40     'i'
         73      > JMP                                                      ->75
         74    >   QM_ASSIGN                                        ~40     ''
         75    >   CONCAT                                           ~41     ~39, ~40
         76        SEND_VAL                                                 ~41
         77        SEND_VAR                                                 !0
         78        DO_ICALL                                         $42     
         79        IS_IDENTICAL                                     ~43     $42, 1
         80        VERIFY_RETURN_TYPE                                       ~43
         81      > RETURN                                                   ~43
   69    82*       VERIFY_RETURN_TYPE                                       
         83*     > 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:  9
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   RECV                                             !0      
   81     1        INIT_FCALL                                               'strtr'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <array>
          4        DO_ICALL                                         $1      
          5        VERIFY_RETURN_TYPE                                       $1
          6      > RETURN                                                   $1
   82     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function escapelike

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.37 ms | 1031 KiB | 25 Q