检查字符串是否包含数字和字母 [英] Check if a string contains numbers and letters

查看:168
本文介绍了检查字符串是否包含数字和字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测一个字符串是否同时包含数字和字母.

I want to detect if a string contains both numbers and letters.

例如:

  • 给出PncC1KECj4pPVW,它将被写入文本文件,因为它包含两者.
  • 给出qdEQ,不会,因为它仅包含字母.
  • Given PncC1KECj4pPVW, it would be written to a text file because it contains both.
  • Given qdEQ, it would not, because it only contains letters.

有没有一种方法可以做到这一点?

Is there a method to do this?

我正在尝试使用

$string = PREG_REPLACE("/[^0-9a-zA-Z]/i", '', $buffer);

但这没用.

任何帮助将不胜感激.

推荐答案

if (preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $myString))
{
    echo 'Secure enough';
}

根据 https://stackoverflow.com/a/9336130/315550 更新的答案,然后将其更新为 https://stackoverflow.com/users/116286/jb

Answer updated based on https://stackoverflow.com/a/9336130/315550, thnx to https://stackoverflow.com/users/116286/jb

这篇关于检查字符串是否包含数字和字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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