Php: preg_match failed to create special character pattern
I have a problem with php regex. I have the same regex in JS and it works. I do not know what's the problem.
this is 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;<||.\'/\
this is the same value just before $ reg_password.
I checked the mysqli method with escape_string, but it doesn't work either:
$password = $this->db->escape_string($password);
echo $password; // 9Gq!Q23Lne;<||.\'/\\
I don't know what my problem is because I used regex101.com to test it and it works.
Any ideas about this? Thanks to
+3
source to share
2 answers
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";
}
0
source to share