php 正则表达式匹配 md5 哈希

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21517102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:13:16  来源:igfitidea点击:

Regex to match md5 hashes

phpregexmatch

提问by mwweb

What type of regex should be used to match a md5 hash.

应该使用什么类型的正则表达式来匹配 md5 哈希。

how to validate this type of string 00236a2ae558018ed13b5222ef1bd987

如何验证这种类型的字符串 00236a2ae558018ed13b5222ef1bd987

i tried something like this: ('/^[a-z0-9]/')but it didnt work.

我试过这样的事情: ('/^[a-z0-9]/')但它没有用。

how to achieve this? thanks

如何实现这一目标?谢谢

回答by Ryan

This is a PCRE that will match a MD5 hash:

这是一个将匹配 MD5 哈希的 PCRE:

define('R_MD5_MATCH', '/^[a-f0-9]{32}$/i');

if(preg_match(R_MD5_MATCH, $input_string)) {
    echo "It matches.";
} else {
    echo "It does not match.";
}

回答by Dave Chen

Try ctype_xdigit:

尝试ctype_xdigit

<?php

$hash = '00236a2ae558018ed13b5222ef1bd987';

var_dump(strlen($hash) === 32 && ctype_xdigit($hash));

Output: bool(true)

输出bool(true)