How to check if string has at least one letter, number and special character in php

后端 未结 6 1466
傲寒
傲寒 2020-12-24 09:23

I am currently writing a small script that checks the contents of each string.

I was wondering what the REGEX would be to make sure that a string has a letter (upper

6条回答
  •  悲&欢浪女
    2020-12-24 10:08

    It may be best to use 3 distinct regexs to do this, since you would need to match 6 different possibilities, depending on where your special characters are in your string. But if you want to do it in one regex, and your special characters are, say, [+?@], it is possible:

    $string = "abc@123";
    $regex = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$%^&]).*$/"
    if (preg_match($regex, $string)) {
       // special characters
    }
    

提交回复
热议问题