Reputation: 1614
I have an issue with php regex. I have same regex in JS and it works. I don't know what is the problem.
this is the php code regex:
echo $password; // 9Gq!Q23Lne;<||.'/\
$reg_password = "/^[-_0-9a-záàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ0\d!@#$%^&*()_\+\{\}:\"<>?\|\[\];\',\.\/\x5c~]{6,30}$/i";
if(isset($password) &&
(!preg_match($reg_password, $password) || !preg_match("#[a-z]#", $password) || !preg_match("#[\d]#", $password))
){
echo "you failed";
}
original password from html input:
9Gq!Q23Lne;<||.\'/\
it is the same value just before the $reg_password.
I have make a test using escape_string from mysqli method but it doesn't work too:
$password = $this->db->escape_string($password);
echo $password; // 9Gq!Q23Lne;<||.\'/\\
I don't know what is my problem because I have used regex101.com to test it and it works..
Any idea about that? Thanks
Upvotes: 3
Views: 262
Reputation: 3081
In your pattern, you need to use single quote instead of double ;)
So it will be like this
$reg_password = '/^[-_0-9a-záàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ0\d!@#$%^&*()_\+\{\}:\"<>?\|\[\];\',\.\/\x5c~]{6,30}$/i';
Upvotes: 2
Reputation: 786291
You don't need to call preg_match
multiple times, just use lookahead to enforce your rules as in this regex:
^(?=.*?\d)(?=.*?[a-z])[-\wáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ0\d!@#$%^&*()+{}:"<>?|\[\];',./\x5c~]{6,30}$
Code:
$re = "`^(?=.*?\\d)(?=.*?[a-z])[-\\wáàâäãåçéèêëíìîïñóòôöõúùûüýÿæœÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝŸÆŒ0\\d!@#$%^&*()+{}:\"<>?|\\[\\];',./\\x5c~]{6,30}$`mu";
$str = "9Gq!Q23Lne;<||.\'/\\";
if (!preg_match($re, $input)) {
echo "you failed";
}
Upvotes: 0