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( '<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_]'; // Create the regexp if (is_array($needle)) { $massagedNeedles = array_map( function($s) use($regexpDelimiter, $wordDelimiter) { $s = preg_quote($s, $regexpDelimiter); $s = str_replace(' ', $wordDelimiter, $s); return $s; }, $needle ); // Sort it reverse so we get the longer strings first usort($massagedNeedles, function($a, $b) { return strlen($b) - strlen($a); }); $regexp = implode('|', $massagedNeedles); } else { $regexp = str_replace(' ', $wordDelimiter, preg_quote($needle, $regexpDelimiter)); } $regexp = sprintf('%1$s%2$s%1$si', $regexpDelimiter, $regexp); // Now find the next occurence if (!preg_match($regexp, $haystack, $matches, PREG_OFFSET_CAPTURE, $offset)) { return $haystack; } $found = array_search(preg_replace('/' . $wordDelimiter . '/', ' ', $matches[0][0]), $needle); $haystack = substr_replace($haystack, $replacement[$found], $matches[0][1], strlen($matches[0][0])); return replaceStrings($haystack, $needle, $replacement, $offset + $matches[0][1] + strlen($replacement[$found])); } var_dump(replaceStrings($message, $search, $replacements));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ohAIp
function name:  (null)
number of ops:  12
compiled vars:  !0 = $message, !1 = $search, !2 = $replacements
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>
   58     3        INIT_FCALL                                               'var_dump'
          4        INIT_FCALL                                               'replacestrings'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        SEND_VAR                                                 !2
          8        DO_FCALL                                      0  $6      
          9        SEND_VAR                                                 $6
         10        DO_ICALL                                                 
         11      > 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 = 35
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 61
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 61
Branch analysis from position: 60
Branch analysis from position: 61
filename:       /in/ohAIp
function name:  replaceStrings
number of ops:  102
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $replacement, !3 = $offset, !4 = $regexpDelimiter, !5 = $wordDelimiter, !6 = $massagedNeedles, !7 = $regexp, !8 = $matches, !9 = $found
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      0
   16     4        COUNT                                            ~10     !1
          5        COUNT                                            ~11     !2
          6        IS_NOT_EQUAL                                             ~10, ~11
          7      > JMPZ                                                     ~12, ->12
   17     8    >   NEW                                              $13     'InvalidArgumentException'
          9        SEND_VAL_EX                                              'Needle+length+must+match+replacement+length'
         10        DO_FCALL                                      0          
         11      > THROW                                         0          $13
   20    12    >   ASSIGN                                                   !4, '%2F'
   21    13        ASSIGN                                                   !5, '%5B-%5Cs_%5D'
   24    14        TYPE_CHECK                                  128          !1
         15      > JMPZ                                                     ~17, ->35
   25    16    >   INIT_FCALL                                               'array_map'
   26    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FohAIp%3A26%240'
         18        BIND_LEXICAL                                             ~18, !4
         19        BIND_LEXICAL                                             ~18, !5
   32    20        SEND_VAL                                                 ~18
   33    21        SEND_VAR                                                 !1
         22        DO_ICALL                                         $19     
   25    23        ASSIGN                                                   !6, $19
   37    24        INIT_FCALL                                               'usort'
         25        SEND_REF                                                 !6
         26        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FohAIp%3A37%241'
         27        SEND_VAL                                                 ~21
         28        DO_ICALL                                                 
   39    29        INIT_FCALL                                               'implode'
         30        SEND_VAL                                                 '%7C'
         31        SEND_VAR                                                 !6
         32        DO_ICALL                                         $23     
         33        ASSIGN                                                   !7, $23
         34      > JMP                                                      ->45
   41    35    >   INIT_FCALL                                               'str_replace'
         36        SEND_VAL                                                 '+'
         37        SEND_VAR                                                 !5
         38        INIT_FCALL                                               'preg_quote'
         39        SEND_VAR                                                 !1
         40        SEND_VAR                                                 !4
         41        DO_ICALL                                         $25     
         42        SEND_VAR                                                 $25
         43        DO_ICALL                                         $26     
         44        ASSIGN                                                   !7, $26
   44    45    >   INIT_FCALL                                               'sprintf'
         46        SEND_VAL                                                 '%251%24s%252%24s%251%24si'
         47        SEND_VAR                                                 !4
         48        SEND_VAR                                                 !7
         49        DO_ICALL                                         $28     
         50        ASSIGN                                                   !7, $28
   47    51        INIT_FCALL                                               'preg_match'
         52        SEND_VAR                                                 !7
         53        SEND_VAR                                                 !0
         54        SEND_REF                                                 !8
         55        SEND_VAL                                                 256
         56        SEND_VAR                                                 !3
         57        DO_ICALL                                         $30     
         58        BOOL_NOT                                         ~31     $30
         59      > JMPZ                                                     ~31, ->61
   48    60    > > RETURN                                                   !0
   51    61    >   INIT_FCALL                                               'array_search'
         62        INIT_FCALL                                               'preg_replace'
         63        CONCAT                                           ~32     '%2F', !5
         64        CONCAT                                           ~33     ~32, '%2F'
         65        SEND_VAL                                                 ~33
         66        SEND_VAL                                                 '+'
         67        FETCH_DIM_R                                      ~34     !8, 0
         68        FETCH_DIM_R                                      ~35     ~34, 0
         69        SEND_VAL                                                 ~35
         70        DO_ICALL                                         $36     
         71        SEND_VAR                                                 $36
         72        SEND_VAR                                                 !1
         73        DO_ICALL                                         $37     
         74        ASSIGN                                                   !9, $37
   53    75        INIT_FCALL                                               'substr_replace'
         76        SEND_VAR                                                 !0
         77        FETCH_DIM_R                                      ~39     !2, !9
         78        SEND_VAL                                                 ~39
         79        FETCH_DIM_R                                      ~40     !8, 0
         80        FETCH_DIM_R                                      ~41     ~40, 1
         81        SEND_VAL                                                 ~41
         82        FETCH_DIM_R                                      ~42     !8, 0
         83        FETCH_DIM_R                                      ~43     ~42, 0
         84        STRLEN                                           ~44     ~43
         85        SEND_VAL                                                 ~44
         86        DO_ICALL                                         $45     
         87        ASSIGN                                                   !0, $45
   55    88        INIT_FCALL_BY_NAME                                       'replaceStrings'
         89        SEND_VAR_EX                                              !0
         90        SEND_VAR_EX                                              !1
         91        SEND_VAR_EX                                              !2
         92        FETCH_DIM_R                                      ~47     !8, 0
         93        FETCH_DIM_R                                      ~48     ~47, 1
         94        ADD                                              ~49     !3, ~48
         95        FETCH_DIM_R                                      ~50     !2, !9
         96        STRLEN                                           ~51     ~50
         97        ADD                                              ~52     ~49, ~51
         98        SEND_VAL_EX                                              ~52
         99        DO_FCALL                                      0  $53     
        100      > RETURN                                                   $53
   56   101*     > RETURN                                                   null

End of function replacestrings

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

End of function %00%7Bclosure%7D%2Fin%2FohAIp%3A26%240

Function %00%7Bclosure%7D%2Fin%2FohAIp%3A37%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ohAIp
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        STRLEN                                           ~2      !1
          3        STRLEN                                           ~3      !0
          4        SUB                                              ~4      ~2, ~3
          5      > RETURN                                                   ~4
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FohAIp%3A37%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.17 ms | 1411 KiB | 36 Q