Friday 17 November 2017

php - Laravel preg_match(): Unknown modifier ']'

itemprop="text">


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
']'


In the following
location:



protected function
validateRegex($attribute, $value, $parameters)
{

$this->requireParameterCount(1, $parameters, 'regex');

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


}

itemprop="text">
class="normal">Answer



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



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

^


Or use other href="http://php.net/manual/en/regexp.reference.delimiters.php"
rel="noreferrer">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 (~).



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...