我正在尝试测试一个字符串,看它是否包含字母数字或标点符号以外的字符,如果包含,则设置错误。我有下面的代码,但它似乎不起作用,因为它让“CZW205é”通过。我对正则表达式感到绝望,似乎无法解决问题。
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;
}
预先感谢您的帮助。
最佳答案
你可以做到
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;
}
[^...]
是一个取反的字符类,一旦发现不在你的类中的东西,它就会匹配。
(因此我删除了 preg_match()
之前的否定)
关于php - 用于匹配标点符号和字母数字字符的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11862129/