简体   繁体   English

CodeIgniter form_validation 中的 regex_match 生成:消息:preg_match():未找到结束分隔符“/”

[英]regex_match in CodeIgniter form_validation generates: Message: preg_match(): No ending delimiter '/' found

I've been looking in other similar posts and the problem seemed to be an unescaped slash.我一直在寻找其他类似的帖子,问题似乎是一个未转义的斜线。 However I'm escaping them.但是我正在逃避他们。

This is how the string should look:这是字符串的外观:

23/12/2012 23/12/2012

and this is how I'm declaring the validation rule:这就是我声明验证规则的方式:

regex_match[/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)[0-9]{2}$/]

The ending delimiter is there, and the two inbetween slashes for the date are being escaped with a backslash.结束定界符在那里,日期的两个中间斜杠用反斜杠转义。 I've also tried this which is slightly different, but I get the same error:我也试过这个略有不同,但我得到了同样的错误:

regex_match[/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/]

Where's the error?错误在哪里?

EDIT:编辑:

Following your advice, I've tried using a callback function.按照您的建议,我尝试使用回调函数。 This is the declaration, which is located within the controller class in which the form validation is being executed:这是声明,它位于执行表单验证的控制器类中:

function mach_date($date) {
   /* DEBUG */ echo 'Here I am!'; exit; // execution should stop here displaying the echo
   return (bool)preg_match('/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/', $date);
}

Validation rules in application/config/form_validation.php : application/config/form_validation.php 中的验证规则:

$config = array(
     // other validation groups.....,
     'articles' => array(
          // other validated fields.....,
          array(
                'field' => 'date_p',
                'label' => 'Publishing date',
                'rules' => 'callback_match_date'
          )
     )
); 

When you set the validation rules you separate them with a |设置验证规则时,用 | 分隔它们。 so the |'s in your regex is causing the validation rule to split at those and that is causing the error.所以正则表达式中的 | 会导致验证规则在那些处拆分,从而导致错误。 Discussion here on the issue. 在这里讨论这个问题。 It seems it's a limitation or bug in codeigniter.这似乎是 codeigniter 中的一个限制或错误。 You can test it out by running a regex with and without |'s and see if the usage of pipes will cause an error.您可以通过运行带和不带 | 的正则表达式来测试它,看看管道的使用是否会导致错误。 If that is the case then you may have to validate by regex by other means, maybe use a callback function as detailed on this page where your function will do a preg_match using the regex which needs to be inside the function of course and then return true/false.如果是这种情况,那么您可能必须通过其他方式通过正则表达式进行验证,也许使用本页面上详述的回调函数,您的函数将使用需要在函数内部的正则表达式进行 preg_match,然后返回 true /错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM