Laravel preg_match(): 未知修饰符 ']'
Laravel preg_match(): Unknown modifier ']'
我正在 Laravel 4.2 工作。我正在尝试使用验证器来验证带有正则表达式的名称字段,这是我的以下规则:
public static $rules_save = [
'name' => 'required|regex:/[XI0-9/]+/|unique:classes'
];
但是一旦我调用要验证的规则,就会抛出错误,请参见下文:
preg_match(): Unknown modifier ']'
在以下位置:
protected function validateRegex($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'regex');
return preg_match($parameters[0], $value); // **ON THIS LINE**
}
由于您需要将 /
包含到字符 class 中,您需要将其转义:
'name' => 'required|regex:/[XI0-9\/]+/|unique:classes'
^
或使用其他regex delimiters.
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 (~
).
正如第一位发帖人 (stribizhev) 所指出的,您需要转义正斜杠 /
,这是因为反斜杠 /
在该模式中用作定界符。因此,使其在字符 class.
中表现得像一个特殊字符
所以你的模式应该是这样的
/[XI0-9\/]+/
但如果您使用其他分隔符,例如 #
,则无需转义正斜杠。
#[XI0-9/]+#
在这里,我没有转义正斜杠,因为我使用 #
作为分隔符
希望对您有所帮助。
有关详细信息,请查看 stribizhev 发布的 link。
我正在 Laravel 4.2 工作。我正在尝试使用验证器来验证带有正则表达式的名称字段,这是我的以下规则:
public static $rules_save = [
'name' => 'required|regex:/[XI0-9/]+/|unique:classes'
];
但是一旦我调用要验证的规则,就会抛出错误,请参见下文:
preg_match(): Unknown modifier ']'
在以下位置:
protected function validateRegex($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'regex');
return preg_match($parameters[0], $value); // **ON THIS LINE**
}
由于您需要将 /
包含到字符 class 中,您需要将其转义:
'name' => 'required|regex:/[XI0-9\/]+/|unique:classes'
^
或使用其他regex delimiters.
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 (~
).
正如第一位发帖人 (stribizhev) 所指出的,您需要转义正斜杠 /
,这是因为反斜杠 /
在该模式中用作定界符。因此,使其在字符 class.
所以你的模式应该是这样的
/[XI0-9\/]+/
但如果您使用其他分隔符,例如 #
,则无需转义正斜杠。
#[XI0-9/]+#
在这里,我没有转义正斜杠,因为我使用 #
作为分隔符
希望对您有所帮助。
有关详细信息,请查看 stribizhev 发布的 link。