Laravel preg_match():未知修饰符']' [英] Laravel preg_match(): Unknown modifier ']'

查看:108
本文介绍了Laravel preg_match():未知修饰符']'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 4.2上工作.我试图使用验证器通过正则表达式验证名称字段,这是我的以下规则:

Im wokring on Laravel 4.2. Im trying to use validator to validate a name field with regex here is my rule below:

public static $rules_save = [
    'name'      => 'required|regex:/[XI0-9/]+/|unique:classes'
];

但是,一旦我调用要验证的规则,就会抛出错误,如下所示:

But as soon as I call the rule to be validated an error is thrown see below:

preg_match(): Unknown modifier ']'

在以下位置:

protected function validateRegex($attribute, $value, $parameters)
    {
        $this->requireParameterCount(1, $parameters, 'regex');

        return preg_match($parameters[0], $value); // **ON THIS LINE**
    }

推荐答案

由于您需要在字符类中包括/,因此您需要对其进行任意设置:

Since you need to include a / into the character class, you need to either esacpe it:

'name'      => 'required|regex:/[XI0-9\/]+/|unique:classes'
                                      ^

或使用其他 regex分隔符.

使用PCRE功能时,要求图案用定界符括起来.分隔符可以是任何非字母数字,非反斜杠,非空格字符.

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

经常使用的定界符是正斜杠(/),哈希符号(#)和波浪号(~).

Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).

这篇关于Laravel preg_match():未知修饰符']'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆