3v4l.org

run code in 300+ PHP versions simultaneously
<?php $message = "This is a string using the terms? Wait here's one\nabukuma 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 = '[- \t_]'; // 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 using the regex we created if (!preg_match($regexp, $haystack, $matches, PREG_OFFSET_CAPTURE, $offset)) { // We've not found one. Let's say we're done. return $haystack; } // We've found one. Clean it up so we can find what replacement it uses $found = array_search(preg_replace('/' . $wordDelimiter . '/', ' ', $matches[0][0]), $needle); if ($found !== false) { $haystack = substr_replace($haystack, $replacement[$found], $matches[0][1], strlen($matches[0][0])); $offset = $matches[0][1] + strlen($replacement[$found]); } else { // This.. shouldn't happen but maybe it will. Let's code a bit defensively. $offset++; } return replaceStrings($haystack, $needle, $replacement, $offset); } var_dump(replaceStrings($message, $search, $replacements));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VLJYT
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%0Aabukuma+kai%0Aabukuma-kai%0Aabukuma%0Aabukuma%0Akai+abukuma+abukuma+kai'
    5     1        ASSIGN                                                   !1, <array>
   10     2        ASSIGN                                                   !2, <array>
   67     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
2 jumps found. (Code = 43) Position 1 = 77, Position 2 = 97
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 97
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/VLJYT
function name:  replaceStrings
number of ops:  106
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-+%5Ct_%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%2FVLJYT%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%2FVLJYT%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
   49    60    > > RETURN                                                   !0
   53    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
   55    75        TYPE_CHECK                                  1018          !9
         76      > JMPZ                                                     ~39, ->97
   56    77    >   INIT_FCALL                                               'substr_replace'
         78        SEND_VAR                                                 !0
         79        FETCH_DIM_R                                      ~40     !2, !9
         80        SEND_VAL                                                 ~40
         81        FETCH_DIM_R                                      ~41     !8, 0
         82        FETCH_DIM_R                                      ~42     ~41, 1
         83        SEND_VAL                                                 ~42
         84        FETCH_DIM_R                                      ~43     !8, 0
         85        FETCH_DIM_R                                      ~44     ~43, 0
         86        STRLEN                                           ~45     ~44
         87        SEND_VAL                                                 ~45
         88        DO_ICALL                                         $46     
         89        ASSIGN                                                   !0, $46
   58    90        FETCH_DIM_R                                      ~48     !8, 0
         91        FETCH_DIM_R                                      ~49     ~48, 1
         92        FETCH_DIM_R                                      ~50     !2, !9
         93        STRLEN                                           ~51     ~50
         94        ADD                                              ~52     ~49, ~51
         95        ASSIGN                                                   !3, ~52
         96      > JMP                                                      ->98
   61    97    >   PRE_INC                                                  !3
   64    98    >   INIT_FCALL_BY_NAME                                       'replaceStrings'
         99        SEND_VAR_EX                                              !0
        100        SEND_VAR_EX                                              !1
        101        SEND_VAR_EX                                              !2
        102        SEND_VAR_EX                                              !3
        103        DO_FCALL                                      0  $55     
        104      > RETURN                                                   $55
   65   105*     > RETURN                                                   null

End of function replacestrings

Function %00%7Bclosure%7D%2Fin%2FVLJYT%3A26%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VLJYT
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%2FVLJYT%3A26%240

Function %00%7Bclosure%7D%2Fin%2FVLJYT%3A37%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VLJYT
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%2FVLJYT%3A37%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.68 ms | 1402 KiB | 36 Q