[thelist] PHP: is_numeric... that's the question...

Max Schwanekamp lists at neptunewebworks.com
Wed Jan 17 12:41:03 CST 2007


Tris wrote:
> =========================
> $codeLength = strlen($usercode);
> $getLast5Characters = substr($usercode, -5, 5);
> $getFirst2Characters = substr($usercode, 0, 2);
> $codeInt = is_numeric($getLast5Characters);
> $codeAlpha = is_numeric($getFirst2Characters);
> if (($codeLength == 7) && ($codeInt) && (!$codeAlpha)) {
> 	return "code valid";
> } else {
> 	return "Code Invalid";
> }
> =========================

I must be blind, because I don't see the error in your code.  However, I 
it might be easier --both to read and to debug-- to use a simple regex here.

function check($usercode) {
	/* valid pattern is 2 letters followed by 5 digits */
	$pattern = '![a-zA-Z]{2}[0-9]{5}!';
	return preg_match($pattern, $usercode);
}

//example
$codes = array('F454354','4G64345','AA12345');
foreach($codes as $c) {
echo "$c is ".(check($c) ? 'valid': 'invalid')."<br>";
}


-- 
Max Schwanekamp
NeptuneWebworks.com
541-255-2171




More information about the thelist mailing list