如何检查字符串在php中是否至少有一个字母、数字和特殊字符 [英] How to check if string has at least one letter, number and special character in php

查看:21
本文介绍了如何检查字符串在php中是否至少有一个字母、数字和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个检查每个字符串内容的小脚本.

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

我想知道 REGEX 是什么来确保字符串具有字母(大写或小写)、数字和特殊字符?

I was wondering what the REGEX would be to make sure that a string has a letter (upper or lower), a digit, and a special character?

这是我目前所知道的(并不多):

Here is what I know so far (whcih isn't much):

if(preg_match('/^[a-zA-Z0-9]+$/i', $string)):

帮助会很棒!

谢谢!

推荐答案

最简单(也可能是最好)的方法是使用 preg_match 进行三个单独的检查:

The easiest (and probably best) way is to do three separate checks with 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中是否至少有一个字母、数字和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆