3v4l.org

run code in 300+ PHP versions simultaneously
<?php class str_replace_except_code { protected static $uniq; protected static $needle; protected static function replaceCB($matches){ return preg_replace("/".self::$uniq."/", self::$needle, $matches[0]); } /** * Replaces all instances of $needle found in $haystack with $replace except where $needle is between [CODE] tags. * @param string $haystack The string to search for instances of $needle * @param string $needle The string to find within $haystack * @param string $replace The replacement value * @return string $haystack with all instances of $needle replaced with $replace where not between [CODE] tags */ public static function go($haystack, $needle, $replace){ //generate a uniq string that shouldn't exist in the haystack self::$uniq = uniqid('UNIQ_', true); self::$needle = $needle; //replace all occurrences of the needle with the unique string $haystack = str_replace($needle, self::$uniq, $haystack); //replace all instances of the unique string between [code] tags with the original string $needle $haystack = preg_replace_callback("/\[CODE[^\]]*\].*?\[\/CODE\]/is", array('str_replace_except_code', 'replaceCB'), $haystack); //replace all remaining unique strings, which should be only those outside [code] tags with the replacement string $haystack = str_replace(self::$uniq, $replace, $haystack); //return the original string with the correct replacements return $haystack; } } $haystack =<<<POST hello /r/n im showing some code /r/n [CODE=HTML] <html> /r/n /r/n </html> [/CODE]/r/n thanks POST; $needle = "/r/n"; $replace= "<br>"; echo '<h1>Before replace:</h1>'; echo '<pre>'.$haystack.'</pre>'; echo '<hr>'; echo '<h1>After replace:</h1>'; echo '<pre>'.str_replace_except_code::go($haystack, $needle, $replace).'</pre>';
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VIktR
function name:  (null)
number of ops:  18
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $replace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   ASSIGN                                                   !0, 'hello+%2Fr%2Fn%0Aim+showing+some+code+%2Fr%2Fn%0A%5BCODE%3DHTML%5D%0A%3Chtml%3E+%2Fr%2Fn%0A%2Fr%2Fn%0A%3C%2Fhtml%3E%0A%5B%2FCODE%5D%2Fr%2Fn%0Athanks'
   48     1        ASSIGN                                                   !1, '%2Fr%2Fn'
   49     2        ASSIGN                                                   !2, '%3Cbr%3E'
   51     3        ECHO                                                     '%3Ch1%3EBefore+replace%3A%3C%2Fh1%3E'
   52     4        CONCAT                                           ~6      '%3Cpre%3E', !0
          5        CONCAT                                           ~7      ~6, '%3C%2Fpre%3E'
          6        ECHO                                                     ~7
   53     7        ECHO                                                     '%3Chr%3E'
   54     8        ECHO                                                     '%3Ch1%3EAfter+replace%3A%3C%2Fh1%3E'
   55     9        INIT_STATIC_METHOD_CALL                                  'str_replace_except_code', 'go'
         10        SEND_VAR                                                 !0
         11        SEND_VAR                                                 !1
         12        SEND_VAR                                                 !2
         13        DO_FCALL                                      0  $8      
         14        CONCAT                                           ~9      '%3Cpre%3E', $8
         15        CONCAT                                           ~10     ~9, '%3C%2Fpre%3E'
         16        ECHO                                                     ~10
         17      > RETURN                                                   1

Class str_replace_except_code:
Function replacecb:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VIktR
function name:  replaceCB
number of ops:  13
compiled vars:  !0 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
    7     1        INIT_FCALL                                               'preg_replace'
          2        FETCH_STATIC_PROP_R          global lock         ~1      'uniq'
          3        CONCAT                                           ~2      '%2F', ~1
          4        CONCAT                                           ~3      ~2, '%2F'
          5        SEND_VAL                                                 ~3
          6        FETCH_STATIC_PROP_R          unknown             ~4      'needle'
          7        SEND_VAL                                                 ~4
          8        FETCH_DIM_R                                      ~5      !0, 0
          9        SEND_VAL                                                 ~5
         10        DO_ICALL                                         $6      
         11      > RETURN                                                   $6
    8    12*     > RETURN                                                   null

End of function replacecb

Function go:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VIktR
function name:  go
number of ops:  33
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $replace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   19     3        INIT_FCALL                                               'uniqid'
          4        SEND_VAL                                                 'UNIQ_'
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $4      
          7        ASSIGN_STATIC_PROP                                       'uniq'
          8        OP_DATA                                                  $4
   20     9        ASSIGN_STATIC_PROP                                       'needle'
         10        OP_DATA                                                  !1
   23    11        INIT_FCALL                                               'str_replace'
         12        SEND_VAR                                                 !1
         13        FETCH_STATIC_PROP_R          unknown             ~6      'uniq'
         14        SEND_VAL                                                 ~6
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $7      
         17        ASSIGN                                                   !0, $7
   26    18        INIT_FCALL                                               'preg_replace_callback'
         19        SEND_VAL                                                 '%2F%5C%5BCODE%5B%5E%5C%5D%5D%2A%5C%5D.%2A%3F%5C%5B%5C%2FCODE%5C%5D%2Fis'
         20        SEND_VAL                                                 <array>
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $9      
         23        ASSIGN                                                   !0, $9
   29    24        INIT_FCALL                                               'str_replace'
         25        FETCH_STATIC_PROP_R          unknown             ~11     'uniq'
         26        SEND_VAL                                                 ~11
         27        SEND_VAR                                                 !2
         28        SEND_VAR                                                 !0
         29        DO_ICALL                                         $12     
         30        ASSIGN                                                   !0, $12
   32    31      > RETURN                                                   !0
   34    32*     > RETURN                                                   null

End of function go

End of class str_replace_except_code.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.87 ms | 1400 KiB | 21 Q