<?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; }
You have javascript disabled. You will not be able to edit any code.