Pregmatch在php中未正确匹配
很难理解这里发生了什么。代码中的注释解释。提前谢谢。为了便于阅读,有些代码位丢失了,比如写入数据库部分。但问题仅限于这些线路Pregmatch在php中未正确匹配,php,regex,if-statement,preg-match,Php,Regex,If Statement,Preg Match,很难理解这里发生了什么。代码中的注释解释。提前谢谢。为了便于阅读,有些代码位丢失了,比如写入数据库部分。但问题仅限于这些线路 if (filter_var($email, FILTER_VALIDATE_EMAIL) == TRUE and preg_match("/^[\w ]+$/", $address_one) == TRUE and preg_match("((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20})", $password)
if (filter_var($email, FILTER_VALIDATE_EMAIL) == TRUE and
preg_match("/^[\w ]+$/", $address_one) == TRUE and
preg_match("((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20})", $password) == TRUE) {
//When given a valid email/address/password the code successfully gets to this point and inserts into a DB
} else {
//When it fails validation with an invalid email/password/address it drops in here
//but then it doesn't change error code variable...
$errorcode = "IHAVENOTCHANGED";
if (filter_var($email, FILTER_VALIDATE_EMAIL) == FALSE) {
$errcode = " email,";
}
if (preg_match("/^[\w ]+$/", $address_one) == FALSE) {
$errcode = " address line 1,";
}
if (preg_match("((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20})", $password) == FALSE) {
$errcode = " password,";
}
writetolog("LoginCreation", "Login failed due to invalid input: " . $errorcode . " - user: " . $email);
echo "Invalid data for $errorcode please try again -- ADDRESS=$address_one -- EMAIL=$email -- PASS=$password";
}您有两个不同的变量
$errorcode
和$errcode
,并且在writetolog()调用中使用前者。如果将这些行放在代码的顶部(并删除$errorcode=“IHAVENOTCHANGED”
),PHP应该抱怨使用了未定义的变量:
error_reporting(-1);
ini_set('display_errors', true);
你还需要用定界符包围你的正则表达式模式——即“代码>/<代码/>字符”,如@ El CLANRS.< /P> < P>除了排除了其他答案已经解决的问题外,为了验证表单数据,考虑关注点的分离。想想看,你有一些字段必须通过一些规则,这是前提。这里有一个简单的例子。免费的codez
// Your rules can be regex or functions
$rules = [
'text' => '/^[\w ]+$/',
'pass' => '/((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20}/',
'email' => function($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
];
function validate($value, $rule)
{
$rule = $rules[$rule];
// A function
if (is_callable($rule)) {
return call_user_func($rule, $value);
}
// A regex
return preg_match($rule, $value);
}
// Usage:
// field => rule
// Use the `name` of your input in the markup
$fields = [
'email' => 'email',
'address' => 'text',
'password' => 'pass'
];
// field => error
$errors = [
'email' => 'Please enter a valid email',
'address' => 'Please enter a valid address',
'password' => 'Please enter a valid password'
];
// Get data from $_POST
$html = [];
foreach ($_POST as $field => $value) {
// We confirm that this is a field we want to process
if (isset($fields[$field])) {
// Did it fail validation?
if ( ! validate($value, $fields[$field])) {
$html []= '<li class="error">'. $errors[$field] .'</li>';
}
}
}
// Print list of errors if any
if ( ! empty($html)) {
echo '<ul id="errors">'. implode('', $html) .'</ul>'
}
exit;
//您的规则可以是正则表达式或函数
$rules=[
“text'=>'/^[\w]+$/”,
'通过'=>'/((?=.*\d)(?=.[a-z])(?=.[a-z])(?=.[@#$%])。{8,20}/',
“电子邮件”=>功能($email){
返回筛选器变量($email,filter\u VALIDATE\u email);
}
];
函数验证($value$rule)
{
$rule=$rules[$rule];
//函数
if(是否可调用($rule)){
返回调用用户函数($rule,$value);
}
//正则表达式
返回preg_match($rule,$value);
}
//用法:
//字段=>规则
//在标记中使用输入的“名称”
$fields=[
“电子邮件”=>“电子邮件”,
'地址'=>'文本',
'密码'=>'通过'
];
//字段=>错误
$errors=[
'电子邮件'=>'请输入有效的电子邮件',
'地址'=>'请输入有效地址',
'密码'=>'请输入有效密码'
];
//从$\u POST获取数据
$html=[];
foreach($\发布为$field=>$value){
//我们确认这是我们要处理的字段
如果(isset($fields[$field])){
//验证失败了吗?
如果(!验证($value,$fields[$field])){
$html[]='。$errors[$field]. ;
}
}
}
//打印错误列表(如果有)
如果(!empty($html)){
echo“”。内爆(“”,$html)。“
”
}
出口
您无法将其进一步隔离?在某些正则表达式中,您似乎缺少分隔符……我的意思是,我想最后两行不是相关信息,但else中的所有3个if语句都不会更改$errorcodePutdie('IHAVENOTCHANGED'))
在else
子句的顶部。你的代码到了吗?当在我的注释上方添加到else中时,它会这样做:“Parse error:syntax error,unexpected T_VARIABLE”(解析错误:语法错误,意外的T_变量)和无效的输入试试看,只需编辑几件事,它将为你提供关于如何组织代码以实现可重用性的新思路。