Php 正则表达式匹配标点符号和字母数字字符
我试图测试一个字符串,看看它是否包含字母数字或标点符号以外的字符,如果包含,则设置一个错误。我有下面的代码,但它似乎不工作,因为它让“CZW205é”通过。我对regex毫无希望,似乎无法解决这个问题Php 正则表达式匹配标点符号和字母数字字符,php,regex,preg-match,Php,Regex,Preg Match,我试图测试一个字符串,看看它是否包含字母数字或标点符号以外的字符,如果包含,则设置一个错误。我有下面的代码,但它似乎不工作,因为它让“CZW205é”通过。我对regex毫无希望,似乎无法解决这个问题 if(!preg_match("/^[a-zA-Z0-9\s\p{P}]/", $product_id)) { $errors[$i] = 'Please only enter alpha-numeric characters, dashes, underscores, colons, f
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;
}
提前感谢你的帮助
/^[a-zA-Z0-9\s\p{P}]+$/
不要忘记用
$
标记字符串的结尾,因为您只匹配第一个字符,请尝试以下代码:
if(preg_match("/[^\w\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;
}
注:\w
是[a-zA-Z0-9.
的缩写
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()
之前删除了否定)非常感谢。很抱歉花了这么长时间回复,但我仍然收到错误500s,所以在我完全相信您的方法有效之前,我正在测试其他错误。还要感谢所有其他解决方案提供商<代码>\w还包括\d
和下划线\u