Regex to match md5 hashes

mwweb picture mwweb · Feb 2, 2014 · Viewed 29.6k times · Source

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

how to validate this type of string 00236a2ae558018ed13b5222ef1bd987

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

how to achieve this? thanks

Answer

Ryan picture Ryan · Feb 2, 2014

This is a PCRE that will match a MD5 hash:

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.";
}