如何区分SHA1字符串和日期时间字符串?

Basically I have two options:

$one = "fdfeb16f096983ada02db49d46a8154475d700ae";
$two = "2011-12-28 05:20:01";

I need some sort of regex, so that I can detect wether the string follows the pattern in $one, or the pattern in $two

Detect if the string is sha1 or datetime.

What would be the best way to determine this?

Thanks

if (preg_match($one, $string) {
    echo "$string matches $one";
} else if (preg_match($two, $string) {
    echo "$string matches $two";
}

Try using the preg_match() function.

http://php.net/manual/en/function.preg-match.php

If you are ABSOLUTE sure those are the ONLY two options, I would go with strlen, and not some kind of marvelous regexp.
Even if those are not the only two options (user messed up), I would still go with strlen, and then check specifically for each format, if it is what you expect it to be.

Conditionals with regex to match each of the options. For the second case:

if(preg_match('|(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}|',$two)
{
   echo "Matching two";
}

I don't see a clear pattern for number one, but you could do elseif(s) to detect other potential cases.

I don't know how to apply regexes in PHP, but assuming the actual regexes are enough...

/^[a-f0-9]{40}$/

Match 40 characters of consisting of a-f or 0-9. The ^ and $ match the beginning and end of the string, so nothing else can be in it.

/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/

Match strings with the date pattern you have.