3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getCopiedText($model, $answer, $min = 4) { //copy original answerstr $modelOriginal = $answer; //replace all space characters from model with single white-space $model = preg_replace('/\s+/iu', ' ', $model); //ensure there are no double spaces in the answer. $answer = preg_replace('/\s+/iu', ' ', $answer); $test = new \CachingIterator(new \ArrayIterator(explode(' ', $answer))); $words = $matches = []; $p = $match = null; //test each word foreach($test as $i => $word) { $words[] = $word; $count = count($words); if ($count === 2) { //save pointer at second word $p = $i; } //check if the phrase of words exists in the model if (false !== stripos($model, $phrase = implode(' ', $words))) { //only match phrases with the minimum or more words if ($count >= $min) { //reset back to here for more matches $match = $phrase; if (!$test->hasNext()) { //add the the last word to the phrase $matches[$match] = true; $p = null; } } } else { //the phrase of words was no longer found if (null !== $match && !isset($matches[$match])) { //add the matched phrase to the list of matches $matches[$match] = true; $p = null; $iterator = $test->getInnerIterator(); if ($iterator->valid()) { //rewind pointer back to the current word since the current word may be part of the next phrase $iterator->seek($i); } } elseif (null !== $p) { //match not found, determine if we need to rewind the pointer $iterator = $test->getInnerIterator(); if ($iterator->valid()) { //rewind pointer back to second word since a partial phrase less than 4 words was matched $iterator->seek($p); } $p = null; } //reset testing $words = []; $match = null; } } //highlight the matched phrases in the answer if (!empty($matches)) { $phrases = array_keys($matches); //sort phrases by the length array_multisort(array_map('strlen', $phrases), $phrases); //filter the matches as regular expression patterns //order by longest phrase first to ensure double highlighting of smaller phrases $phrases = array_map(function($phrase) { return '/(' . preg_quote($phrase, '/') . ')/iu'; }, array_reverse($phrases)); $modelOriginal = preg_replace($phrases, '<span style="color:red">$0</span>', $modelOriginal); } return $modelOriginal; } $model ="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; $answer = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; echo getCopiedText($model,$answer);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3pBFR
function name:  (null)
number of ops:  8
compiled vars:  !0 = $model, !1 = $answer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   ASSIGN                                                   !0, 'Lorem+Ipsum+is+simply+dummy+text+of+the+printing+and+typesetting+industry.+Lorem+Ipsum+has+been+the+industry%27s+standard+dummy+text+ever+since+the+1500s%2C+when+an+unknown+printer+took+a+galley+of+type+and+scrambled+it+to+make+a+type+specimen+book.+It+has+survived+not+only+five+centuries%2C+but+also+the+leap+into+electronic+typesetting%2C+remaining+essentially+unchanged.+It+was+popularised+in+the+1960s+with+the+release+of+Letraset+sheets+containing+Lorem+Ipsum+passages%2C+and+more+recently+with+desktop+publishing+software+like+Aldus+PageMaker+including+versions+of+Lorem+Ipsum.'
   80     1        ASSIGN                                                   !1, 'Lorem+Ipsum+is+simply+dummy+text+of+the+printing+and+typesetting+industry.+It+is+a+long+established+fact+that+a+reader+will+be+distracted+by+the+readable+content+of+a+page+when+looking+at+its+layout.+%0AIt+has+survived+not+only+five+centuries%2C+but+also+the+leap+into++electronic+typesetting%2C+remaining+essentially+unchanged.+%0AIt+was+popularised+in+the+1960s+with+the+release+of+Letraset+sheets+containing+Lorem+Ipsum+passages%2C+and+more+recently+with+desktop+publishing+software+like+Aldus+PageMaker+including+versions+of+Lorem+Ipsum.'
   83     2        INIT_FCALL                                               'getcopiedtext'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
          7      > RETURN                                                   1

Function getcopiedtext:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 97
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 97
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 41
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 63
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 62
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 62
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 96
Branch analysis from position: 96
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 62
Branch analysis from position: 62
Branch analysis from position: 63
2 jumps found. (Code = 46) Position 1 = 65, Position 2 = 68
Branch analysis from position: 65
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 82
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 78, Position 2 = 81
Branch analysis from position: 78
1 jumps found. (Code = 42) Position 1 = 94
Branch analysis from position: 94
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 81
Branch analysis from position: 82
2 jumps found. (Code = 43) Position 1 = 84, Position 2 = 94
Branch analysis from position: 84
2 jumps found. (Code = 43) Position 1 = 90, Position 2 = 93
Branch analysis from position: 90
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 93
Branch analysis from position: 94
Branch analysis from position: 68
Branch analysis from position: 41
Branch analysis from position: 97
2 jumps found. (Code = 43) Position 1 = 101, Position 2 = 128
Branch analysis from position: 101
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 128
Branch analysis from position: 97
filename:       /in/3pBFR
function name:  getCopiedText
number of ops:  130
compiled vars:  !0 = $model, !1 = $answer, !2 = $min, !3 = $modelOriginal, !4 = $test, !5 = $words, !6 = $matches, !7 = $p, !8 = $match, !9 = $word, !10 = $i, !11 = $count, !12 = $phrase, !13 = $iterator, !14 = $phrases
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      4
    7     3        ASSIGN                                                   !3, !1
    9     4        INIT_FCALL                                               'preg_replace'
          5        SEND_VAL                                                 '%2F%5Cs%2B%2Fiu'
          6        SEND_VAL                                                 '+'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $16     
          9        ASSIGN                                                   !0, $16
   11    10        INIT_FCALL                                               'preg_replace'
         11        SEND_VAL                                                 '%2F%5Cs%2B%2Fiu'
         12        SEND_VAL                                                 '+'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $18     
         15        ASSIGN                                                   !1, $18
   13    16        NEW                                              $20     'CachingIterator'
         17        NEW                                              $21     'ArrayIterator'
         18        INIT_FCALL                                               'explode'
         19        SEND_VAL                                                 '+'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                         $22     
         22        SEND_VAR_NO_REF_EX                                       $22
         23        DO_FCALL                                      0          
         24        SEND_VAR_NO_REF_EX                                       $21
         25        DO_FCALL                                      0          
         26        ASSIGN                                                   !4, $20
   14    27        ASSIGN                                           ~26     !6, <array>
         28        ASSIGN                                                   !5, ~26
   15    29        ASSIGN                                           ~28     !8, null
         30        ASSIGN                                                   !7, ~28
   17    31      > FE_RESET_R                                       $30     !4, ->97
         32    > > FE_FETCH_R                                       ~31     $30, !9, ->97
         33    >   ASSIGN                                                   !10, ~31
   18    34        ASSIGN_DIM                                               !5
         35        OP_DATA                                                  !9
   19    36        COUNT                                            ~34     !5
         37        ASSIGN                                                   !11, ~34
   20    38        IS_IDENTICAL                                             !11, 2
         39      > JMPZ                                                     ~36, ->41
   22    40    >   ASSIGN                                                   !7, !10
   25    41    >   INIT_FCALL                                               'stripos'
         42        SEND_VAR                                                 !0
         43        INIT_FCALL                                               'implode'
         44        SEND_VAL                                                 '+'
         45        SEND_VAR                                                 !5
         46        DO_ICALL                                         $38     
         47        ASSIGN                                           ~39     !12, $38
         48        SEND_VAL                                                 ~39
         49        DO_ICALL                                         $40     
         50        TYPE_CHECK                                  1018          $40
         51      > JMPZ                                                     ~41, ->63
   27    52    >   IS_SMALLER_OR_EQUAL                                      !2, !11
         53      > JMPZ                                                     ~42, ->62
   29    54    >   ASSIGN                                                   !8, !12
   30    55        INIT_METHOD_CALL                                         !4, 'hasNext'
         56        DO_FCALL                                      0  $44     
         57        BOOL_NOT                                         ~45     $44
         58      > JMPZ                                                     ~45, ->62
   32    59    >   ASSIGN_DIM                                               !6, !8
         60        OP_DATA                                                  <true>
   33    61        ASSIGN                                                   !7, null
   25    62    > > JMP                                                      ->96
   38    63    >   TYPE_CHECK                                  1020  ~48     !8
         64      > JMPZ_EX                                          ~48     ~48, ->68
         65    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~49     !6, !8
         66        BOOL_NOT                                         ~50     ~49
         67        BOOL                                             ~48     ~50
         68    > > JMPZ                                                     ~48, ->82
   40    69    >   ASSIGN_DIM                                               !6, !8
         70        OP_DATA                                                  <true>
   41    71        ASSIGN                                                   !7, null
   42    72        INIT_METHOD_CALL                                         !4, 'getInnerIterator'
         73        DO_FCALL                                      0  $53     
         74        ASSIGN                                                   !13, $53
   43    75        INIT_METHOD_CALL                                         !13, 'valid'
         76        DO_FCALL                                      0  $55     
         77      > JMPZ                                                     $55, ->81
   45    78    >   INIT_METHOD_CALL                                         !13, 'seek'
         79        SEND_VAR_EX                                              !10
         80        DO_FCALL                                      0          
   38    81    > > JMP                                                      ->94
   47    82    >   TYPE_CHECK                                  1020          !7
         83      > JMPZ                                                     ~57, ->94
   49    84    >   INIT_METHOD_CALL                                         !4, 'getInnerIterator'
         85        DO_FCALL                                      0  $58     
         86        ASSIGN                                                   !13, $58
   50    87        INIT_METHOD_CALL                                         !13, 'valid'
         88        DO_FCALL                                      0  $60     
         89      > JMPZ                                                     $60, ->93
   52    90    >   INIT_METHOD_CALL                                         !13, 'seek'
         91        SEND_VAR_EX                                              !7
         92        DO_FCALL                                      0          
   54    93    >   ASSIGN                                                   !7, null
   57    94    >   ASSIGN                                                   !5, <array>
   58    95        ASSIGN                                                   !8, null
   17    96    > > JMP                                                      ->32
         97    >   FE_FREE                                                  $30
   63    98        ISSET_ISEMPTY_CV                                 ~65     !6
         99        BOOL_NOT                                         ~66     ~65
        100      > JMPZ                                                     ~66, ->128
   64   101    >   INIT_FCALL                                               'array_keys'
        102        SEND_VAR                                                 !6
        103        DO_ICALL                                         $67     
        104        ASSIGN                                                   !14, $67
   66   105        INIT_FCALL                                               'array_multisort'
        106        INIT_FCALL                                               'array_map'
        107        SEND_VAL                                                 'strlen'
        108        SEND_VAR                                                 !14
        109        DO_ICALL                                         $69     
        110        SEND_VAL                                                 $69
        111        SEND_REF                                                 !14
        112        DO_ICALL                                                 
   70   113        INIT_FCALL                                               'array_map'
        114        DECLARE_LAMBDA_FUNCTION                          ~71     [0]
   72   115        SEND_VAL                                                 ~71
        116        INIT_FCALL                                               'array_reverse'
        117        SEND_VAR                                                 !14
        118        DO_ICALL                                         $72     
        119        SEND_VAR                                                 $72
   70   120        DO_ICALL                                         $73     
        121        ASSIGN                                                   !14, $73
   74   122        INIT_FCALL                                               'preg_replace'
        123        SEND_VAR                                                 !14
        124        SEND_VAL                                                 '%3Cspan+style%3D%22color%3Ared%22%3E%240%3C%2Fspan%3E'
        125        SEND_VAR                                                 !3
        126        DO_ICALL                                         $75     
        127        ASSIGN                                                   !3, $75
   77   128    > > RETURN                                                   !3
   78   129*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3pBFR
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $phrase
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
   71     1        INIT_FCALL                                               'preg_quote'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 '%2F'
          4        DO_ICALL                                         $1      
          5        CONCAT                                           ~2      '%2F%28', $1
          6        CONCAT                                           ~3      ~2, '%29%2Fiu'
          7      > RETURN                                                   ~3
   72     8*     > RETURN                                                   null

End of Dynamic Function 0

End of function getcopiedtext

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.17 ms | 1039 KiB | 23 Q