Question

I have a program which create a random number with MH{random alphanumeric}K format.

eg : MHmnwfJHJ1234K

How to check if the random number is in this format?

Was it helpful?

Solution 2

You could use regular expressions like so:

<?php
if(preg_match('/MH[0-9]+K/', 'MH123K')) {
    echo "Match!";
}

This one allows for any random number.

OTHER TIPS

Something like this?

preg_match('/MH(\d+)K/', $subject, $matches)

http://nl3.php.net/preg_match

Assume that you have your generated string(number) in $randNum variable. The simplest way would be this (but not the best way):

if($randNum[0]=="M" && $randNum[1]=="H" && $randNum[strlen($randNum)-1]=="K")

But the best way would be using regular expressions as @Allendar posted above me:

preg_match('/MH(\d+)K/', $subject, $matches)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top