3v4l.org

run code in 500+ PHP versions simultaneously
<?php $string = "To be escaped: + - = && || > < ! ( ) { } [ ] ^ \" ~ * ? : \ / triple ||| and split '&<&'"; print_r( [ $string, LouisBarranqueiro($string), MikkoRantalainen($string), mickmackusa($string) ] ); function LouisBarranqueiro($string) { $regex = "/[\\+\\-\\=\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\\"\\~\\*\\<\\>\\?\\:\\\\\\/]/"; $string = preg_replace_callback ($regex, function ($matches) { return "\\" . $matches[0]; }, $string); return $string; } function MikkoRantalainen($s) { static $replacements = array(); if (!$replacements) { # https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters $replacements = array( "\\" => "\\\\", # must be done first to not double encode later backslashes! "+" => "\\+", "-" => "\\-", "=" => "\\=", "&" => "\\&", "|" => "\\|", ">" => "", # cannot be safely encoded "<" => "", # cannot be safely encoded "!" => "\\!", "(" => "\\(", ")" => "\\)", "{" => "\\{", "}" => "\\}", "[" => "\\[", "]" => "\\]", "^" => "\\^", "\"" => "\\\"", "~" => "\\~", "*" => "\\*", "?" => "\\?", ":" => "\\:", "/" => "\\/", ); } return str_replace(array_keys($replacements), array_values($replacements), $s); } function mickmackusa($string) { return preg_replace( [ '_[<>]+_', // prevent hack where double-symbol is split by gt/lt symbol '_[-+=!(){}[\]^"~*?:\\/\\\\]|&(?=&)|\|(?=\|)_', ], [ '', '\\\\$0', ], $string ); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Qa43R
function name:  (null)
number of ops:  18
compiled vars:  !0 = $string
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, 'To+be+escaped%3A+%2B+-+%3D+%26%26+%7C%7C+%3E+%3C+%21+%28+%29+%7B+%7D+%5B+%5D+%5E+%22+%7E+%2A+%3F+%3A+%5C+%2F+triple+%7C%7C%7C+and+split+%27%26%3C%26%27'
    5     1        INIT_FCALL                                                   'print_r'
    7     2        INIT_ARRAY                                           ~2      !0
    8     3        INIT_FCALL_BY_NAME                                           'LouisBarranqueiro'
          4        SEND_VAR_EX                                                  !0
          5        DO_FCALL                                          0  $3      
          6        ADD_ARRAY_ELEMENT                                    ~2      $3
    9     7        INIT_FCALL_BY_NAME                                           'MikkoRantalainen'
          8        SEND_VAR_EX                                                  !0
          9        DO_FCALL                                          0  $4      
         10        ADD_ARRAY_ELEMENT                                    ~2      $4
   10    11        INIT_FCALL_BY_NAME                                           'mickmackusa'
         12        SEND_VAR_EX                                                  !0
         13        DO_FCALL                                          0  $5      
         14        ADD_ARRAY_ELEMENT                                    ~2      $5
         15        SEND_VAL                                                     ~2
    5    16        DO_ICALL                                                     
   69    17      > RETURN                                                       1

Function louisbarranqueiro:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Qa43R
function name:  LouisBarranqueiro
number of ops:  11
compiled vars:  !0 = $string, !1 = $regex
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
   15     1        ASSIGN                                                       !1, '%2F%5B%5C%2B%5C-%5C%3D%5C%26%5C%7C%5C%21%5C%28%5C%29%5C%7B%5C%7D%5C%5B%5C%5D%5C%5E%5C%22%5C%7E%5C%2A%5C%3C%5C%3E%5C%3F%5C%3A%5C%5C%5C%2F%5D%2F'
   16     2        INIT_FCALL                                                   'preg_replace_callback'
          3        SEND_VAR                                                     !1
   17     4        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
   19     5        SEND_VAL                                                     ~3
          6        SEND_VAR                                                     !0
   16     7        DO_ICALL                                             $4      
          8        ASSIGN                                                       !0, $4
   20     9      > RETURN                                                       !0
   21    10*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Qa43R
function name:  {closure:LouisBarranqueiro():17}
number of ops:  5
compiled vars:  !0 = $matches
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   17     0  E >   RECV                                                 !0      
   18     1        FETCH_DIM_R                                          ~1      !0, 0
          2        CONCAT                                               ~2      '%5C', ~1
          3      > RETURN                                                       ~2
   19     4*     > RETURN                                                       null

End of Dynamic Function 0

End of function louisbarranqueiro

Function mikkorantalainen:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/Qa43R
function name:  MikkoRantalainen
number of ops:  15
compiled vars:  !0 = $s, !1 = $replacements
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
   25     1        BIND_STATIC                                                  !1
   26     2        BOOL_NOT                                             ~2      !1
          3      > JMPZ                                                         ~2, ->5
   29     4    >   ASSIGN                                                       !1, <array>
   54     5    >   INIT_FCALL                                                   'array_keys'
          6        SEND_VAR                                                     !1
          7        DO_ICALL                                             $4      
          8        INIT_FCALL                                                   'array_values'
          9        SEND_VAR                                                     !1
         10        DO_ICALL                                             $5      
         11        FRAMELESS_ICALL_3                str_replace         ~6      $4, $5
         12        OP_DATA                                                      !0
         13      > RETURN                                                       ~6
   55    14*     > RETURN                                                       null

End of function mikkorantalainen

Function mickmackusa:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Qa43R
function name:  mickmackusa
number of ops:  5
compiled vars:  !0 = $string
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   57     0  E >   RECV                                                 !0      
   58     1        FRAMELESS_ICALL_3                preg_replace        ~1      <array>, <array>
   67     2        OP_DATA                                                      !0
          3      > RETURN                                                       ~1
   69     4*     > RETURN                                                       null

End of function mickmackusa

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
171.68 ms | 2101 KiB | 17 Q