PHP - preg_match 混淆 - 没有检查正确的字符集 - 需要空格
PHP - preg_match confusion - not checking for correct character set - needs spaces
我目前正在构建一个将在其中包含地址的应用程序,我正在使用 preg_match 进行一些字符检测,并在用户使用无效字符时以用户的方式抛出错误以确保其安全.
我的问题是使用 preg_match 它似乎表现得很奇怪,我认为我没有正确使用它,因为它的行为方式。
我下面的代码应该允许字母 A 到 Z,城市、县和国家的大写和小写。下面的代码是更新该字段时城市的示例:
// The user wants to update the city so let's go through the update process
if ( isset($_POST['city']) ) {
// Check that the city is different before we try to update it
if ( $newcompanycity != $companycity ) {
// Check that the city is less than 50 characters and contains valid characters
if ( (strlen($newcompanycity) <= 50) && (preg_match('/[^A-Za-z]/', $newcompanycity)) ) {
// The city is fine so let's update it
$update_success = true;
mysqli_query($sql, "UPDATE clients SET city = '$newcompanycity' WHERE companyid = '$companyid'") or die(mysqli_error($sql));
} else {
// The city doesn't meet the requirements to be update so return an error
$update_fail = true;
$city_error = true;
}
}
}
现在的问题是 $city_error
在当前值为 "Sheffield" 时被触发,而您将其更改为 "York" 它 returns 作为 $city_error
变量变为真。然而,将值从 "Sheffield" 更改为 "Sheffield 1",然后它会工作并更新数据库。
我是不是漏掉了什么?我认为 A-Za-z 只检查字母,如果只有字母那么它应该可以工作。但这似乎根本不起作用。
在我发布这篇文章之前只是快速更新一下。我刚刚意识到我需要在字符串末尾添加一个 space 然后它就可以工作了。我真的很困惑。因此,如果没有 space 它 returns 一个错误,因为 preg_match 不允许它但是使用 space 即使它没有在 preg_match 内部定义它允许它。这肯定不是正常行为?
有时正则表达式只会让事情变得更复杂。 PHP 有一个很棒的函数叫做 ctype_alpha()
,它会检查一个变量是否只有 A-Za-z。
(ctype_alpha($newcompanycity))
您的正则表达式是 /[^A-Za-z]/
,意思类似于 "everything that is not A-Z and a-z"。
preg_match 函数 return 匹配的数量,所以如果没有发现无效字符,它应该 return 0.
因此,如果您将 (preg_match('/[^A-Za-z]/', $newcompanycity))
更改为 (preg_match('/[^A-Za-z]/', $newcompanycity) === 0)
,它应该会按预期工作,因为如果没有找到无效字符,它就会变为真。
要包含空格,只需将它们添加到您的正则表达式中即可:/[^A-Za-z ]/
。
我目前正在构建一个将在其中包含地址的应用程序,我正在使用 preg_match 进行一些字符检测,并在用户使用无效字符时以用户的方式抛出错误以确保其安全.
我的问题是使用 preg_match 它似乎表现得很奇怪,我认为我没有正确使用它,因为它的行为方式。
我下面的代码应该允许字母 A 到 Z,城市、县和国家的大写和小写。下面的代码是更新该字段时城市的示例:
// The user wants to update the city so let's go through the update process
if ( isset($_POST['city']) ) {
// Check that the city is different before we try to update it
if ( $newcompanycity != $companycity ) {
// Check that the city is less than 50 characters and contains valid characters
if ( (strlen($newcompanycity) <= 50) && (preg_match('/[^A-Za-z]/', $newcompanycity)) ) {
// The city is fine so let's update it
$update_success = true;
mysqli_query($sql, "UPDATE clients SET city = '$newcompanycity' WHERE companyid = '$companyid'") or die(mysqli_error($sql));
} else {
// The city doesn't meet the requirements to be update so return an error
$update_fail = true;
$city_error = true;
}
}
}
现在的问题是 $city_error
在当前值为 "Sheffield" 时被触发,而您将其更改为 "York" 它 returns 作为 $city_error
变量变为真。然而,将值从 "Sheffield" 更改为 "Sheffield 1",然后它会工作并更新数据库。
我是不是漏掉了什么?我认为 A-Za-z 只检查字母,如果只有字母那么它应该可以工作。但这似乎根本不起作用。
在我发布这篇文章之前只是快速更新一下。我刚刚意识到我需要在字符串末尾添加一个 space 然后它就可以工作了。我真的很困惑。因此,如果没有 space 它 returns 一个错误,因为 preg_match 不允许它但是使用 space 即使它没有在 preg_match 内部定义它允许它。这肯定不是正常行为?
有时正则表达式只会让事情变得更复杂。 PHP 有一个很棒的函数叫做 ctype_alpha()
,它会检查一个变量是否只有 A-Za-z。
(ctype_alpha($newcompanycity))
您的正则表达式是 /[^A-Za-z]/
,意思类似于 "everything that is not A-Z and a-z"。
preg_match 函数 return 匹配的数量,所以如果没有发现无效字符,它应该 return 0.
因此,如果您将 (preg_match('/[^A-Za-z]/', $newcompanycity))
更改为 (preg_match('/[^A-Za-z]/', $newcompanycity) === 0)
,它应该会按预期工作,因为如果没有找到无效字符,它就会变为真。
要包含空格,只需将它们添加到您的正则表达式中即可:/[^A-Za-z ]/
。