3v4l.org

run code in 300+ PHP versions simultaneously
<?php $message = "This is a string using the terms? Wait here's one\n abukuma kai\nabukuma-kai\nabukuma\nabukuma\nkai abukuma abukuma kai"; $search = array( 'abukuma kai', 'abukuma' ); $replacements = array( // here all replaces | each in a new line - watch the syntax // all html possible, but no single quotation mark! '<a href="http://kancolle.wikia.com/wiki/Abukuma#Upgrade" class="screenshot" rel="ship/Abukuma_Kai.png" style="color:#0073e6; font-weight:bold; border-bottom: 1px #0073e6 dashed;"><p id="yosup">Abukuma Kai</p></a>', '<a href="http://kancolle.wikia.com/wiki/Abukuma#Basic" class="screenshot" rel="ship/Abukuma.png" style="color:#0073e6; font-weight:bold; border-bottom: 1px #0073e6 dashed;">Abukuma</a>' ); function replaceStrings($haystack, $needle, $replacement, $offset = 0) { if (count($needle) != count($replacement)) { throw new \InvalidArgumentException('Needle length must match replacement length'); } $regexpDelimiter = '/'; $wordDelimiter = '[-\s_]'; if (is_array($needle)) { $massagedNeedles = array_map( function($s) use($regexpDelimiter, $wordDelimiter) { $s = preg_quote($s, $regexpDelimiter); $s = preg_replace('/' . $wordDelimiter . '/', $wordDelimiter, $s); return $s; }, $needle ); $regexp = implode('|', $massagedNeedes); } else { $regexp = preg_replace('/' . $wordDelimiter . '/', preg_quote($needle, '/')); } $regexp = sprintf('/%s/i', $regexp); $strpos = preg_match($regexp, $haystack, $matches, 0, $offset); if ($strpos === false) { return $haystack; } var_dump($matches); exit; return replaceStrings($haystack, $needle, $replacement, $offset); } replaceStrings($message, $search, $replace);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PkLWU
function name:  (null)
number of ops:  9
compiled vars:  !0 = $message, !1 = $search, !2 = $replacements, !3 = $replace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'This+is+a+string+using+the+terms%3F++Wait+here%27s+one%0A+abukuma+kai%0Aabukuma-kai%0Aabukuma%0Aabukuma%0Akai+abukuma+abukuma+kai'
    5     1        ASSIGN                                                   !1, <array>
   10     2        ASSIGN                                                   !2, <array>
   56     3        INIT_FCALL                                               'replacestrings'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        SEND_VAR                                                 !3
          7        DO_FCALL                                      0          
          8      > RETURN                                                   1

Function replacestrings:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 30
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 57
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 57
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 57
Branch analysis from position: 56
Branch analysis from position: 57
filename:       /in/PkLWU
function name:  replaceStrings
number of ops:  69
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $replacement, !3 = $offset, !4 = $regexpDelimiter, !5 = $wordDelimiter, !6 = $massagedNeedles, !7 = $regexp, !8 = $massagedNeedes, !9 = $strpos, !10 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      0
   18     4        COUNT                                            ~11     !1
          5        COUNT                                            ~12     !2
          6        IS_NOT_EQUAL                                             ~11, ~12
          7      > JMPZ                                                     ~13, ->12
   19     8    >   NEW                                              $14     'InvalidArgumentException'
          9        SEND_VAL_EX                                              'Needle+length+must+match+replacement+length'
         10        DO_FCALL                                      0          
         11      > THROW                                         0          $14
   22    12    >   ASSIGN                                                   !4, '%2F'
   23    13        ASSIGN                                                   !5, '%5B-%5Cs_%5D'
   25    14        TYPE_CHECK                                  128          !1
         15      > JMPZ                                                     ~18, ->30
   26    16    >   INIT_FCALL                                               'array_map'
   27    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPkLWU%3A27%240'
         18        BIND_LEXICAL                                             ~19, !4
         19        BIND_LEXICAL                                             ~19, !5
   33    20        SEND_VAL                                                 ~19
   34    21        SEND_VAR                                                 !1
         22        DO_ICALL                                         $20     
   26    23        ASSIGN                                                   !6, $20
   37    24        INIT_FCALL                                               'implode'
         25        SEND_VAL                                                 '%7C'
         26        SEND_VAR                                                 !8
         27        DO_ICALL                                         $22     
         28        ASSIGN                                                   !7, $22
         29      > JMP                                                      ->41
   39    30    >   INIT_FCALL                                               'preg_replace'
         31        CONCAT                                           ~24     '%2F', !5
         32        CONCAT                                           ~25     ~24, '%2F'
         33        SEND_VAL                                                 ~25
         34        INIT_FCALL                                               'preg_quote'
         35        SEND_VAR                                                 !1
         36        SEND_VAL                                                 '%2F'
         37        DO_ICALL                                         $26     
         38        SEND_VAR                                                 $26
         39        DO_ICALL                                         $27     
         40        ASSIGN                                                   !7, $27
   42    41    >   INIT_FCALL                                               'sprintf'
         42        SEND_VAL                                                 '%2F%25s%2Fi'
         43        SEND_VAR                                                 !7
         44        DO_ICALL                                         $29     
         45        ASSIGN                                                   !7, $29
   44    46        INIT_FCALL                                               'preg_match'
         47        SEND_VAR                                                 !7
         48        SEND_VAR                                                 !0
         49        SEND_REF                                                 !10
         50        SEND_VAL                                                 0
         51        SEND_VAR                                                 !3
         52        DO_ICALL                                         $31     
         53        ASSIGN                                                   !9, $31
   46    54        TYPE_CHECK                                    4          !9
         55      > JMPZ                                                     ~33, ->57
   47    56    > > RETURN                                                   !0
   50    57    >   INIT_FCALL                                               'var_dump'
         58        SEND_VAR                                                 !10
         59        DO_ICALL                                                 
   51    60      > EXIT                                                     
   53    61*       INIT_FCALL_BY_NAME                                       'replaceStrings'
         62*       SEND_VAR_EX                                              !0
         63*       SEND_VAR_EX                                              !1
         64*       SEND_VAR_EX                                              !2
         65*       SEND_VAR_EX                                              !3
         66*       DO_FCALL                                      0  $35     
         67*       RETURN                                                   $35
   54    68*     > RETURN                                                   null

End of function replacestrings

Function %00%7Bclosure%7D%2Fin%2FPkLWU%3A27%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/PkLWU
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $s, !1 = $regexpDelimiter, !2 = $wordDelimiter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   28     3        INIT_FCALL                                               'preg_quote'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $3      
          7        ASSIGN                                                   !0, $3
   30     8        INIT_FCALL                                               'preg_replace'
          9        CONCAT                                           ~5      '%2F', !2
         10        CONCAT                                           ~6      ~5, '%2F'
         11        SEND_VAL                                                 ~6
         12        SEND_VAR                                                 !2
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $7      
         15        ASSIGN                                                   !0, $7
   32    16      > RETURN                                                   !0
   33    17*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FPkLWU%3A27%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.65 ms | 1411 KiB | 28 Q