我目前正在编写一个小脚本来检查每个字符串的内容。
我想知道 REGEX 是什么来确保一个字符串有一个字母(大写字母或小写字母)、一个数字和一个特殊字符?
这是我目前所知道的(并不多):
if(preg_match('/^[a-zA-Z0-9]+$/i', $string)):
帮助会很棒!
谢谢!
最佳答案
最简单(也可能是最好)的方法是使用 preg_match
进行三个单独的检查:
$containsLetter = preg_match('/[a-zA-Z]/', $string);
$containsDigit = preg_match('/\d/', $string);
$containsSpecial = preg_match('/[^a-zA-Z\d]/', $string);
// $containsAll = $containsLetter && $containsDigit && $containsSpecial
关于php - php判断字符串是否至少有一个字母、数字和特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9587907/