3v4l.org

run code in 300+ PHP versions simultaneously
<?php $patterns = [ 'you suck' => 'And *you* like it.', 'how are you(?: (?:doing|today))?' => 'I\'m fine how are you?', '(?:.*)? you are (a(?:n)?) ([^\b]+)' => 'No *you* are $1 $2', '^\?$' => 'What?', '^(wtf|wth|defak|thefuck|the fuck|dafuq)' => 'What? I only execute commands. Go blame somebody else.', 'give (?:my|your|me my) (.*) back' => '/me gives $1 back.', '(?:thank you|thanks|thks|tnx|thx|^ta$)' => 'You\'re welcome!', '(?:you dead|are you dead|you are dead|dead)' => 'Nope. Not that I know of...', '(?:hi|hey|heya|yo|hello|hellow|hola)^' => 'Hola', '(?:are )?you drunk' => 'Screw you human!', 'are you (?:ok|fine|alive|working)' => 'Yeah I\'m fine thanks.', 'are you (?:busy|available)' => 'What do you need?', '^(?:what|wat)$' => 'What what?', '♥|love|<3' => 'I love you too :-)', 'your (?:mother|mom|momma|mommy|mummy|mum)' => 'My mother at least acknowledged me as her child.', '(?:that\'s|that is|you\'re|you are|you)( .*)? (?:awesome|great|cool|nice|awesomesauce|perfect|the best)' => 'I know right!', 'you(.*)? sentient' => 'No no no. I am just a dumb bot. Carry one ---filthy human--- errrr master.', '^what are you doing' => 'Nothing much. You?', '^(what|who) are you' => 'I\'m a bot.', 'ask you (:?something|a question)' => 'Sure. Shoot.', 'can you do something' => 'What do you want me to do?', 'can you do (?:a trick|tricks)' => 'Type this code in your chat window: `<(?:"[^"]*"[\'"]*|\'[^\']*\'[\'"]*|[^\'">])+>`', 'what do you think (?:of|about) me' => 'You\'re ok.', 'what do you think (?:of|about) cap(.*)' => 'It\'s ok for a first prototype I guess.', 'what do you think (?:of|about) (?:singletons|globals|javascript|js|node|mongo|laravel)' => 'It\'s crap and should be avoided.', 'did you try (?:singletons|globals|javascript|js|node|mongo|laravel)(?: yet)?' => 'Yes. It\'s crap and should be avoided.', 'what (?:do you think)? (?:of|about) jquery' => 'It\'s great and does all the things!', '(?:.*)? what do you think (?:of|about) (.*)' => 'I don\'t think I like $1', '(?:.*)? what\'s your opinion on (.*)' => 'I don\'t think I like $1', 'what about (?:.*)' => 'What about it?', '^why' => 'Because', '(?:What is|What\'s the meaning of life)' => '42', '(?:Are )you a (?:ro)?bot' => 'Step aside you filthy human.', ]; function normalizeText(string $text) { return str_replace('?', '.', (trim(strtolower($text)))); } function isSpecialCased(string $message, array $patterns): bool { foreach ($patterns as $pattern => $response) { if (preg_match('/' . $pattern . '/iu', normalizeText($message)) === 1) { return true; } } return false; } function isMatch(string $message, string $bot = 'jeeves'): bool { return text_contains_ping($message, $bot); } function text_contains_ping(string $text, string $userName = 'jeeves'): bool { $userName = \preg_quote(\preg_replace('#\s+#', '', $userName)); $expr = \sprintf('((?<=^|\s)@(%s)(?=[\s,.\'"?!;:<>\#@~{}%%^&*-]|$))iu', $userName); return (bool)\preg_match($expr, $text); } function text_strip_pings(string $text, string $userName = 'jeeves', string $replacement = ''): string { $userName = \preg_quote(\preg_replace('#\s+#', '', $userName)); $expr = \sprintf('((?<=^|\s)@(%s)(?=[\s,.\'"?!;:<>\#@~{}%%^&*-]|$))iu', $userName); return \preg_replace($expr, $replacement, $text); } function getResponse(string $message, array $patterns): string { foreach ($patterns as $pattern => $response) { if (preg_match('/' . $pattern . '/iu', normalizeText($message)) === 1) { return buildResponse($pattern, $response, $message); } } return ''; } function buildResponse(string $pattern, string $response, string $conversationText): string { if (strpos($response, '$1') !== false) { return preg_replace('/' . $pattern . '/iu', $response, normalizeText($conversationText)); } return $response; } echo $message = '@Jeeves but you are an elephpant. I think you\'re having a identity crisis?'; #var_dump(text_contains_ping($message, 'jeeves')); #var_dump(text_contains_ping($message, 'Jeeves')); #var_dump(text_strip_pings($message, 'Jeeves')); #var_dump(isMatch($message, 'Jeeves')); var_dump(getResponse($message, $patterns)); echo $msg = "@Jeeves what do you think of @Ekin?\n"; #var_dump(isMatch($msg, 'Jeeves')); var_dump(getResponse($msg, $patterns)); echo $msg = "but what do you think of @PeeHaa, @jeeves?\n"; var_dump(text_strip_pings($msg, 'jeeves')); var_dump(getResponse($msg, $patterns));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ettVS
function name:  (null)
number of ops:  36
compiled vars:  !0 = $patterns, !1 = $message, !2 = $msg
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   95     1        ASSIGN                                           ~4      !1, '%40Jeeves+but+you+are+an+elephpant.+I+think+you%27re+having+a+identity+crisis%3F'
          2        ECHO                                                     ~4
  100     3        INIT_FCALL                                               'var_dump'
          4        INIT_FCALL                                               'getresponse'
          5        SEND_VAR                                                 !1
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0  $5      
          8        SEND_VAR                                                 $5
          9        DO_ICALL                                                 
  102    10        ASSIGN                                           ~7      !2, '%40Jeeves+what+do+you+think+of+%40Ekin%3F%0A'
         11        ECHO                                                     ~7
  104    12        INIT_FCALL                                               'var_dump'
         13        INIT_FCALL                                               'getresponse'
         14        SEND_VAR                                                 !2
         15        SEND_VAR                                                 !0
         16        DO_FCALL                                      0  $8      
         17        SEND_VAR                                                 $8
         18        DO_ICALL                                                 
  106    19        ASSIGN                                           ~10     !2, 'but+what+do+you+think+of+%40PeeHaa%2C+%40jeeves%3F%0A'
         20        ECHO                                                     ~10
  107    21        INIT_FCALL                                               'var_dump'
         22        INIT_FCALL                                               'text_strip_pings'
         23        SEND_VAR                                                 !2
         24        SEND_VAL                                                 'jeeves'
         25        DO_FCALL                                      0  $11     
         26        SEND_VAR                                                 $11
         27        DO_ICALL                                                 
  108    28        INIT_FCALL                                               'var_dump'
         29        INIT_FCALL                                               'getresponse'
         30        SEND_VAR                                                 !2
         31        SEND_VAR                                                 !0
         32        DO_FCALL                                      0  $13     
         33        SEND_VAR                                                 $13
         34        DO_ICALL                                                 
         35      > RETURN                                                   1

Function normalizetext:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ettVS
function name:  normalizeText
number of ops:  14
compiled vars:  !0 = $text
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
   40     1        INIT_FCALL                                               'str_replace'
          2        SEND_VAL                                                 '%3F'
          3        SEND_VAL                                                 '.'
          4        INIT_FCALL                                               'trim'
          5        INIT_FCALL                                               'strtolower'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $1      
          8        SEND_VAR                                                 $1
          9        DO_ICALL                                         $2      
         10        SEND_VAR                                                 $2
         11        DO_ICALL                                         $3      
         12      > RETURN                                                   $3
   41    13*     > RETURN                                                   null

End of function normalizetext

Function isspecialcased:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 19
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/ettVS
function name:  isSpecialCased
number of ops:  23
compiled vars:  !0 = $message, !1 = $patterns, !2 = $response, !3 = $pattern
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   45     2      > FE_RESET_R                                       $4      !1, ->19
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->19
          4    >   ASSIGN                                                   !3, ~5
   46     5        INIT_FCALL                                               'preg_match'
          6        CONCAT                                           ~7      '%2F', !3
          7        CONCAT                                           ~8      ~7, '%2Fiu'
          8        SEND_VAL                                                 ~8
          9        INIT_FCALL                                               'normalizetext'
         10        SEND_VAR                                                 !0
         11        DO_FCALL                                      0  $9      
         12        SEND_VAR                                                 $9
         13        DO_ICALL                                         $10     
         14        IS_IDENTICAL                                             $10, 1
         15      > JMPZ                                                     ~11, ->18
   47    16    >   FE_FREE                                                  $4
         17      > RETURN                                                   <true>
   45    18    > > JMP                                                      ->3
         19    >   FE_FREE                                                  $4
   51    20      > RETURN                                                   <false>
   52    21*       VERIFY_RETURN_TYPE                                       
         22*     > RETURN                                                   null

End of function isspecialcased

Function ismatch:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ettVS
function name:  isMatch
number of ops:  10
compiled vars:  !0 = $message, !1 = $bot
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'jeeves'
   56     2        INIT_FCALL_BY_NAME                                       'text_contains_ping'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $2      
          6        VERIFY_RETURN_TYPE                                       $2
          7      > RETURN                                                   $2
   57     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null

End of function ismatch

Function text_contains_ping:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ettVS
function name:  text_contains_ping
number of ops:  25
compiled vars:  !0 = $text, !1 = $userName, !2 = $expr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'jeeves'
   61     2        INIT_FCALL                                               'preg_quote'
          3        INIT_FCALL                                               'preg_replace'
          4        SEND_VAL                                                 '%23%5Cs%2B%23'
          5        SEND_VAL                                                 ''
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $3      
          8        SEND_VAR                                                 $3
          9        DO_ICALL                                         $4      
         10        ASSIGN                                                   !1, $4
   62    11        INIT_FCALL                                               'sprintf'
         12        SEND_VAL                                                 '%28%28%3F%3C%3D%5E%7C%5Cs%29%40%28%25s%29%28%3F%3D%5B%5Cs%2C.%27%22%3F%21%3B%3A%3C%3E%5C%23%40%7E%7B%7D%25%25%5E%26%2A-%5D%7C%24%29%29iu'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $6      
         15        ASSIGN                                                   !2, $6
   64    16        INIT_FCALL                                               'preg_match'
         17        SEND_VAR                                                 !2
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $8      
         20        BOOL                                             ~9      $8
         21        VERIFY_RETURN_TYPE                                       ~9
         22      > RETURN                                                   ~9
   65    23*       VERIFY_RETURN_TYPE                                       
         24*     > RETURN                                                   null

End of function text_contains_ping

Function text_strip_pings:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ettVS
function name:  text_strip_pings
number of ops:  26
compiled vars:  !0 = $text, !1 = $userName, !2 = $replacement, !3 = $expr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'jeeves'
          2        RECV_INIT                                        !2      ''
   69     3        INIT_FCALL                                               'preg_quote'
          4        INIT_FCALL                                               'preg_replace'
          5        SEND_VAL                                                 '%23%5Cs%2B%23'
          6        SEND_VAL                                                 ''
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $4      
          9        SEND_VAR                                                 $4
         10        DO_ICALL                                         $5      
         11        ASSIGN                                                   !1, $5
   70    12        INIT_FCALL                                               'sprintf'
         13        SEND_VAL                                                 '%28%28%3F%3C%3D%5E%7C%5Cs%29%40%28%25s%29%28%3F%3D%5B%5Cs%2C.%27%22%3F%21%3B%3A%3C%3E%5C%23%40%7E%7B%7D%25%25%5E%26%2A-%5D%7C%24%29%29iu'
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $7      
         16        ASSIGN                                                   !3, $7
   72    17        INIT_FCALL                                               'preg_replace'
         18        SEND_VAR                                                 !3
         19        SEND_VAR                                                 !2
         20        SEND_VAR                                                 !0
         21        DO_ICALL                                         $9      
         22        VERIFY_RETURN_TYPE                                       $9
         23      > RETURN                                                   $9
   73    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function text_strip_pings

Function getresponse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 25
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 24
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/ettVS
function name:  getResponse
number of ops:  29
compiled vars:  !0 = $message, !1 = $patterns, !2 = $response, !3 = $pattern
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   75     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   77     2      > FE_RESET_R                                       $4      !1, ->25
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->25
          4    >   ASSIGN                                                   !3, ~5
   78     5        INIT_FCALL                                               'preg_match'
          6        CONCAT                                           ~7      '%2F', !3
          7        CONCAT                                           ~8      ~7, '%2Fiu'
          8        SEND_VAL                                                 ~8
          9        INIT_FCALL                                               'normalizetext'
         10        SEND_VAR                                                 !0
         11        DO_FCALL                                      0  $9      
         12        SEND_VAR                                                 $9
         13        DO_ICALL                                         $10     
         14        IS_IDENTICAL                                             $10, 1
         15      > JMPZ                                                     ~11, ->24
   79    16    >   INIT_FCALL_BY_NAME                                       'buildResponse'
         17        SEND_VAR_EX                                              !3
         18        SEND_VAR_EX                                              !2
         19        SEND_VAR_EX                                              !0
         20        DO_FCALL                                      0  $12     
         21        VERIFY_RETURN_TYPE                                       $12
         22        FE_FREE                                                  $4
         23      > RETURN                                                   $12
   77    24    > > JMP                                                      ->3
         25    >   FE_FREE                                                  $4
   83    26      > RETURN                                                   ''
   84    27*       VERIFY_RETURN_TYPE                                       
         28*     > RETURN                                                   null

End of function getresponse

Function buildresponse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 21
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ettVS
function name:  buildResponse
number of ops:  25
compiled vars:  !0 = $pattern, !1 = $response, !2 = $conversationText
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   88     3        INIT_FCALL                                               'strpos'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 '%241'
          6        DO_ICALL                                         $3      
          7        TYPE_CHECK                                  1018          $3
          8      > JMPZ                                                     ~4, ->21
   89     9    >   INIT_FCALL                                               'preg_replace'
         10        CONCAT                                           ~5      '%2F', !0
         11        CONCAT                                           ~6      ~5, '%2Fiu'
         12        SEND_VAL                                                 ~6
         13        SEND_VAR                                                 !1
         14        INIT_FCALL                                               'normalizetext'
         15        SEND_VAR                                                 !2
         16        DO_FCALL                                      0  $7      
         17        SEND_VAR                                                 $7
         18        DO_ICALL                                         $8      
         19        VERIFY_RETURN_TYPE                                       $8
         20      > RETURN                                                   $8
   92    21    >   VERIFY_RETURN_TYPE                                       !1
         22      > RETURN                                                   !1
   93    23*       VERIFY_RETURN_TYPE                                       
         24*     > RETURN                                                   null

End of function buildresponse

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
125.96 ms | 1465 KiB | 29 Q