3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getCopiedText($model, $answer, $min = 4) { //ensure there are not double spaces $model = str_replace(' ', ' ', $model); $answer = str_replace(' ', ' ', $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 match $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 a partial phrase less than 4 words was matched $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)); $answer = preg_replace($phrases, '<span style="color:blue">$0</span>', $answer); } return $answer; } $modelAnswer = "Lorem Ipsum is simply dummy \ntext of the printing and typesetting industry. Lorem Ipsum has been the industry`s standard dummy text ever since the 1500s, when an unknown printer\ntook 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."; $answers = [ 'matches' => "NOT IN is simply dummy \ntext NOT in when an unknown printer took a galley -this- is simply dummy text of the printing and typesetting industry", 'matches2' => "NOT IN is simply dummy text NOT in when an unknown printer\ntook a galley -this- is simply dummy text of the printing and typesetting industry", 'break-mismatch' => "NOT IN is simply dummy \r\ntext NOT in when an unknown printer took a galley -this- is simply dummy text of the printing and typesetting industry", 'does-not-match' => "NOT IN is simply dummy\n text NOT in when an unknown printer took a galley -this- is simply dummy text of the printing and typesetting industry" ]; foreach($answers as $key => $answer) { echo $key . ': ' . \PHP_EOL; echo getCopiedText($modelAnswer, $answer); echo \PHP_EOL . '-----' . \PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/Pi5Hi
function name:  (null)
number of ops:  17
compiled vars:  !0 = $modelAnswer, !1 = $answers, !2 = $answer, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   ASSIGN                                                   !0, 'Lorem+Ipsum+is+simply+dummy+%0Atext+of+the+printing+and+typesetting+industry.+Lorem+Ipsum+has+been+the+industry%60s+standard+dummy+text+ever+since+the+1500s%2C+when+an+unknown+printer%0Atook+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.'
   77     1        ASSIGN                                                   !1, <array>
   84     2      > FE_RESET_R                                       $6      !1, ->15
          3    > > FE_FETCH_R                                       ~7      $6, !2, ->15
          4    >   ASSIGN                                                   !3, ~7
   85     5        CONCAT                                           ~9      !3, '%3A+'
          6        CONCAT                                           ~10     ~9, '%0A'
          7        ECHO                                                     ~10
   86     8        INIT_FCALL                                               'getcopiedtext'
          9        SEND_VAR                                                 !0
         10        SEND_VAR                                                 !2
         11        DO_FCALL                                      0  $11     
         12        ECHO                                                     $11
   87    13        ECHO                                                     '%0A-----%0A'
   84    14      > JMP                                                      ->3
         15    >   FE_FREE                                                  $6
   88    16      > RETURN                                                   1

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

End of Dynamic Function 0

End of function getcopiedtext

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.65 ms | 1029 KiB | 24 Q