I am trying to test a string to see if it contains characters other than alphanumeric or punctuation and, if it does, set an error. I have the code below, but it doesn’t seem to work as it’s letting “CZW205é” pass. I’m hopeless at regex and can’t seem to resolve the problem.
if(!preg_match("/^[a-zA-Z0-9\s\p{P}]/", $product_id)) {
$errors[$i] = 'Please only enter alpha-numeric characters, dashes, underscores, colons, full-stops, apostrophes, brackets, commas and forward slashes';
continue;
}
Thanks in advance for your help.
You can do
[^...]is a negated character class, it will match as soon as it finds something that is not inside your class.(And therefor I removed the negation before the
preg_match())