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_]'; 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 ); usort($massagedNeedles, function($a, $b) { return strlen($b) - strlen($a); }); var_dump($massagedNeedles); $regexp = implode('|', $massagedNeedles); } else { $regexp = preg_replace('/' . $wordDelimiter . '/', preg_quote($needle, '/')); } $regexp = sprintf('/%s/i', $regexp); $strpos = preg_match($regexp, $haystack, $matches, PREG_OFFSET_CAPTURE, $offset); if ($strpos === false) { return $haystack; } // $haystack = substr_replace($haystack, $replacement[$i], $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/cXvTO
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>
   60     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 = 38
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 43) Position 1 = 64, Position 2 = 65
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 64, Position 2 = 65
Branch analysis from position: 64
Branch analysis from position: 65
filename:       /in/cXvTO
function name:  replaceStrings
number of ops:  74
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $replacement, !3 = $offset, !4 = $regexpDelimiter, !5 = $wordDelimiter, !6 = $massagedNeedles, !7 = $regexp, !8 = $strpos, !9 = $matches
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'
   23    14        TYPE_CHECK                                  128          !1
         15      > JMPZ                                                     ~17, ->38
   24    16    >   INIT_FCALL                                               'array_map'
   25    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcXvTO%3A25%240'
         18        BIND_LEXICAL                                             ~18, !4
         19        BIND_LEXICAL                                             ~18, !5
   31    20        SEND_VAL                                                 ~18
   32    21        SEND_VAR                                                 !1
         22        DO_ICALL                                         $19     
   24    23        ASSIGN                                                   !6, $19
   35    24        INIT_FCALL                                               'usort'
         25        SEND_REF                                                 !6
         26        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FcXvTO%3A35%241'
         27        SEND_VAL                                                 ~21
         28        DO_ICALL                                                 
   37    29        INIT_FCALL                                               'var_dump'
         30        SEND_VAR                                                 !6
         31        DO_ICALL                                                 
   39    32        INIT_FCALL                                               'implode'
         33        SEND_VAL                                                 '%7C'
         34        SEND_VAR                                                 !6
         35        DO_ICALL                                         $24     
         36        ASSIGN                                                   !7, $24
         37      > JMP                                                      ->49
   41    38    >   INIT_FCALL                                               'preg_replace'
         39        CONCAT                                           ~26     '%2F', !5
         40        CONCAT                                           ~27     ~26, '%2F'
         41        SEND_VAL                                                 ~27
         42        INIT_FCALL                                               'preg_quote'
         43        SEND_VAR                                                 !1
         44        SEND_VAL                                                 '%2F'
         45        DO_ICALL                                         $28     
         46        SEND_VAR                                                 $28
         47        DO_ICALL                                         $29     
         48        ASSIGN                                                   !7, $29
   44    49    >   INIT_FCALL                                               'sprintf'
         50        SEND_VAL                                                 '%2F%25s%2Fi'
         51        SEND_VAR                                                 !7
         52        DO_ICALL                                         $31     
         53        ASSIGN                                                   !7, $31
   46    54        INIT_FCALL                                               'preg_match'
         55        SEND_VAR                                                 !7
         56        SEND_VAR                                                 !0
         57        SEND_REF                                                 !9
         58        SEND_VAL                                                 256
         59        SEND_VAR                                                 !3
         60        DO_ICALL                                         $33     
         61        ASSIGN                                                   !8, $33
   48    62        TYPE_CHECK                                    4          !8
         63      > JMPZ                                                     ~35, ->65
   49    64    > > RETURN                                                   !0
   55    65    > > EXIT                                                     
   57    66*       INIT_FCALL_BY_NAME                                       'replaceStrings'
         67*       SEND_VAR_EX                                              !0
         68*       SEND_VAR_EX                                              !1
         69*       SEND_VAR_EX                                              !2
         70*       SEND_VAR_EX                                              !3
         71*       DO_FCALL                                      0  $36     
         72*       RETURN                                                   $36
   58    73*     > RETURN                                                   null

End of function replacestrings

Function %00%7Bclosure%7D%2Fin%2FcXvTO%3A25%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cXvTO
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $s, !1 = $regexpDelimiter, !2 = $wordDelimiter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   26     3        INIT_FCALL                                               'preg_quote'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $3      
          7        ASSIGN                                                   !0, $3
   28     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
   30    16      > RETURN                                                   !0
   31    17*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FcXvTO%3A25%240

Function %00%7Bclosure%7D%2Fin%2FcXvTO%3A35%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cXvTO
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     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%2FcXvTO%3A35%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.41 ms | 1407 KiB | 30 Q