3v4l.org

run code in 300+ PHP versions simultaneously
<?php $bads = [ '+11', '- 68', '[img', '$cool ', '# hash' ]; $tests = []; foreach ($bads as $bad) { $tests["A $bad B"] = true; $tests["$bad"] = true; $tests["A $bad"] = true; $tests["$bad B"] = true; $tests["x${bad}x"] = false; $tests["x${bad}"] = false; $tests["${bad}x"] = false; } foreach ($tests as $string => $expected) { $expect = (int)$expected; $actual = (int)contains($string, $bads); if ($actual !== $expect) { echo "FAIL: '$string' should be $expect, is $actual"; } else { echo "PASS: '$string' was expected $expect"; } echo PHP_EOL; } #### FUNCTION ### function contains($str, $bads) { $template = '/(\s+%1$s\s+|^\s*%1$s\s+|\s+%1$s\s*$|^\s*%1$s\s*$)/'; foreach ($bads as $a) { $regex = sprintf($template, preg_quote($a, '/')); if ($x = preg_match($regex, $str, $matches)) { return true; } } return false; }
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /in/RH6r3 on line 11 Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /in/RH6r3 on line 12 Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /in/RH6r3 on line 13 PASS: 'A +11 B' was expected 1 PASS: '+11' was expected 1 PASS: 'A +11' was expected 1 PASS: '+11 B' was expected 1 PASS: 'x+11x' was expected 0 PASS: 'x+11' was expected 0 PASS: '+11x' was expected 0 PASS: 'A - 68 B' was expected 1 PASS: '- 68' was expected 1 PASS: 'A - 68' was expected 1 PASS: '- 68 B' was expected 1 PASS: 'x- 68x' was expected 0 PASS: 'x- 68' was expected 0 PASS: '- 68x' was expected 0 PASS: 'A [img B' was expected 1 PASS: '[img' was expected 1 PASS: 'A [img' was expected 1 PASS: '[img B' was expected 1 PASS: 'x[imgx' was expected 0 PASS: 'x[img' was expected 0 PASS: '[imgx' was expected 0 PASS: 'A $cool B' was expected 1 PASS: '$cool ' was expected 1 PASS: 'A $cool ' was expected 1 PASS: '$cool B' was expected 1 PASS: 'x$cool x' was expected 0 PASS: 'x$cool ' was expected 0 PASS: '$cool x' was expected 0 PASS: 'A # hash B' was expected 1 PASS: '# hash' was expected 1 PASS: 'A # hash' was expected 1 PASS: '# hash B' was expected 1 PASS: 'x# hashx' was expected 0 PASS: 'x# hash' was expected 0 PASS: '# hashx' was expected 0
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33
PASS: 'A +11 B' was expected 1 PASS: '+11' was expected 1 PASS: 'A +11' was expected 1 PASS: '+11 B' was expected 1 PASS: 'x+11x' was expected 0 PASS: 'x+11' was expected 0 PASS: '+11x' was expected 0 PASS: 'A - 68 B' was expected 1 PASS: '- 68' was expected 1 PASS: 'A - 68' was expected 1 PASS: '- 68 B' was expected 1 PASS: 'x- 68x' was expected 0 PASS: 'x- 68' was expected 0 PASS: '- 68x' was expected 0 PASS: 'A [img B' was expected 1 PASS: '[img' was expected 1 PASS: 'A [img' was expected 1 PASS: '[img B' was expected 1 PASS: 'x[imgx' was expected 0 PASS: 'x[img' was expected 0 PASS: '[imgx' was expected 0 PASS: 'A $cool B' was expected 1 PASS: '$cool ' was expected 1 PASS: 'A $cool ' was expected 1 PASS: '$cool B' was expected 1 PASS: 'x$cool x' was expected 0 PASS: 'x$cool ' was expected 0 PASS: '$cool x' was expected 0 PASS: 'A # hash B' was expected 1 PASS: '# hash' was expected 1 PASS: 'A # hash' was expected 1 PASS: '# hash B' was expected 1 PASS: 'x# hashx' was expected 0 PASS: 'x# hash' was expected 0 PASS: '# hashx' was expected 0

preferences:
190.9 ms | 413 KiB | 5 Q