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"; $msg = text_strip_pings($msg, 'jeeves'); var_dump(getResponse($msg, $patterns));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.40.0040.01619.07
8.3.30.0040.01119.01
8.3.20.0040.00420.32
8.3.10.0050.00322.08
8.3.00.0000.00823.79
8.2.170.0150.00022.96
8.2.160.0120.00322.33
8.2.150.0060.00324.18
8.2.140.0060.00324.66
8.2.130.0080.00020.64
8.2.120.0040.00426.35
8.2.110.0040.00721.25
8.2.100.0040.00818.47
8.2.90.0000.01019.47
8.2.80.0060.00318.00
8.2.70.0070.00317.88
8.2.60.0050.00518.18
8.2.50.0060.00618.28
8.2.40.0060.00320.71
8.2.30.0050.00521.25
8.2.20.0040.00418.37
8.2.10.0030.00518.33
8.2.00.0000.00818.27
8.1.270.0090.00020.62
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0040.00722.62
8.1.230.0030.00920.28
8.1.220.0030.00617.80
8.1.210.0030.00618.77
8.1.200.0030.00717.60
8.1.190.0070.00317.47
8.1.180.0030.00518.10
8.1.170.0000.00918.65
8.1.160.0030.00618.96
8.1.150.0040.00420.29
8.1.140.0000.00817.87
8.1.130.0000.00719.13
8.1.120.0000.00817.71
8.1.110.0080.00017.75
8.1.100.0040.00417.63
8.1.90.0000.01217.63
8.1.80.0000.00817.63
8.1.70.0000.00817.61
8.1.60.0000.01017.88
8.1.50.0000.00817.79
8.1.40.0090.00017.68
8.1.30.0080.00017.78
8.1.20.0040.00417.82
8.1.10.0040.00417.93
8.1.00.0030.00617.86
8.0.300.0090.00018.77
8.0.290.0040.00417.00
8.0.280.0030.00618.56
8.0.270.0050.00317.45
8.0.260.0070.00018.78
8.0.250.0000.00917.31
8.0.240.0030.00517.37
8.0.230.0040.00417.35
8.0.220.0040.00417.31
8.0.210.0080.00017.19
8.0.200.0040.00417.21
8.0.190.0000.00917.38
8.0.180.0030.00517.33
8.0.170.0050.00317.29
8.0.160.0030.00517.34
8.0.150.0060.00617.24
8.0.140.0030.00617.20
8.0.130.0030.00613.74
8.0.120.0040.00417.16
8.0.110.0060.00317.29
8.0.100.0040.00417.18
8.0.90.0040.00417.17
8.0.80.0130.00317.29
8.0.70.0040.00417.23
8.0.60.0000.00817.23
8.0.50.0030.00517.28
8.0.30.0100.01117.54
8.0.20.0100.01217.50
8.0.10.0000.00817.32
8.0.00.0060.01316.94
7.4.330.0000.00515.55
7.4.320.0030.00316.89
7.4.300.0040.00416.92
7.4.290.0040.00416.99
7.4.280.0040.00416.82
7.4.270.0000.00817.06
7.4.260.0040.00416.88
7.4.250.0040.00416.99
7.4.240.0060.00316.92
7.4.230.0040.00416.88
7.4.220.0100.01716.94
7.4.210.0100.00616.93
7.4.200.0030.00616.88
7.4.160.0030.01516.77
7.4.150.0130.00617.40
7.4.140.0120.01117.86
7.4.130.0070.01417.03
7.4.120.0110.01016.95
7.4.110.0100.00816.80
7.4.100.0070.01316.82
7.4.90.0120.00616.78
7.4.80.0110.00819.39
7.4.70.0040.01716.79
7.4.60.0100.00816.94
7.4.50.0090.00916.82
7.4.40.0110.00716.99
7.4.30.0070.01116.77
7.4.00.0030.01615.49
7.3.330.0000.00613.54
7.3.320.0060.00013.43
7.3.310.0040.00416.65
7.3.300.0000.00816.65
7.3.290.0060.00916.62
7.3.280.0100.01016.62
7.3.270.0130.00617.40
7.3.260.0050.01416.90
7.3.250.0040.01616.77
7.3.240.0080.01116.70
7.3.230.0150.00416.98
7.3.210.0060.01316.81
7.3.200.0140.01016.68
7.3.190.0120.00916.96
7.3.180.0030.01416.81
7.3.170.0000.01816.88
7.3.160.0090.00916.86
7.3.120.0090.00615.30
7.3.110.0060.00915.27
7.3.100.0070.01114.99
7.3.90.0070.01015.37
7.3.80.0040.01115.04
7.3.70.0060.00615.24
7.3.60.0120.00314.82
7.3.50.0090.00614.94
7.3.40.0090.00615.20
7.3.30.0030.01315.30
7.3.20.0100.01016.81
7.3.10.0060.01016.72
7.3.00.0040.01116.89
7.2.330.0130.00617.00
7.2.320.0110.00716.93
7.2.310.0060.01216.94
7.2.300.0030.01617.03
7.2.290.0140.01016.91
7.2.240.0130.00015.13
7.2.230.0030.01015.49
7.2.220.0120.00415.45
7.2.210.0000.01515.07
7.2.200.0000.01515.39
7.2.190.0030.00915.51
7.2.180.0030.01315.46
7.2.170.0040.01415.37
7.2.160.0040.01115.03
7.2.150.0070.01417.17
7.2.140.0090.00917.19
7.2.130.0060.01516.95
7.2.120.0060.01517.13
7.2.110.0080.01016.89
7.2.100.0070.01017.22
7.2.90.0040.01217.23
7.2.80.0130.00317.09
7.2.70.0080.01217.21
7.2.60.0100.00817.29
7.2.50.0160.00617.22
7.2.40.0180.00417.27
7.2.30.0090.00917.25
7.2.20.0150.00717.08
7.2.10.0130.00717.22
7.2.00.0030.01317.20
7.1.330.0100.00715.88
7.1.320.0030.01016.03
7.1.310.0070.00715.96
7.1.300.0000.01715.99
7.1.290.0070.00715.89
7.1.280.0030.01315.78
7.1.270.0090.00916.08
7.1.260.0000.01715.91
7.1.250.0190.00615.96
7.1.200.0060.00815.97
7.1.100.2590.01316.52
7.1.90.2640.01316.45
7.1.80.2620.01016.73
7.1.70.2690.00715.58
7.1.60.2860.01333.14
7.1.50.2970.01032.72
7.1.40.2490.01432.57
7.1.30.3340.01732.51
7.1.20.3370.01032.70
7.1.10.2220.01615.18
7.1.00.2560.01315.11
7.0.240.2790.01016.12
7.0.230.2570.01616.30
7.0.220.2760.01016.13
7.0.210.2740.01015.03
7.0.200.2980.00315.39
7.0.190.2600.01015.16
7.0.180.2780.00314.85
7.0.170.3550.01314.85
7.0.160.3420.00714.72
7.0.150.2200.01714.73
7.0.140.2140.01014.93
7.0.130.3300.01315.04
7.0.120.3300.00314.91
7.0.110.3300.01014.77
7.0.100.2950.01014.90
7.0.90.3200.01314.84
7.0.80.2970.00714.92
7.0.70.2940.01014.73
7.0.60.2380.00714.71
7.0.50.3470.01314.82
7.0.40.4210.02014.90
7.0.30.2630.01014.92
7.0.20.2640.01314.98
7.0.10.2260.01014.87
7.0.00.2160.00714.69

preferences:
49.09 ms | 400 KiB | 5 Q