3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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"; $msg = text_strip_pings($msg, 'jeeves'); var_dump(getResponse($msg, $patterns));
based on ettVS
Output for hhvm-3.18.5 - 3.22.0, 7.0.0 - 7.3.0beta1
@Jeeves but you are an elephpant. I think you're having a identity crisis?string(67) "No *you* are an elephpant. i think you're having a identity crisis." @Jeeves what do you think of @Ekin? string(27) "I don't think I like @ekin." but what do you think of @PeeHaa, @jeeves? string(31) "I don't think I like @peehaa, ."
Output for 5.6.0 - 5.6.30
Parse error: syntax error, unexpected ':', expecting '{' in /in/W4rVB on line 43
Process exited with code 255.