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

End of function replacestrings

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

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

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.01 ms | 1411 KiB | 34 Q