Page 1 of 1

dynamically passing comparison operator

Posted: Sat Nov 05, 2005 6:16 am
by raghavan20
I am developing a custom function to check whether an input string is alphanumeric and the length of string matches the required length. It should satisfy any of the operators depending on what the user needs like (>, <, >=, == )

Code: Select all

//This works
<?php
function check_alphanumeric_length($input_string, $length, $comparison_operator = ""){
	$valid = 0; //initially the input assumed to be invalid
	if(ereg("^[a-zA-Z0-9]*$", $input_string)){
		echo "<br />"."check_alphanumeric_length: Input - $input_string is a alphanumeric";
		if ($comparison_operator == ""){
			if (strlen($input_string) <= $length){
				$valid = 1;				
				echo "<br />"."check_alphanumeric_length: compared length:".strlen($input_string)."; comparison operator:<=; String is of valid length";
			}
		}else{
			switch ($comparison_operator){
				case "==":
					if (strlen($input_string) == $length) $valid = 1;
					break;
				case "<":
					if (strlen($input_string) < $length) $valid = 1;
					break;
				case ">":
					if (strlen($input_string) > $length) $valid = 1;
					break;
				case ">=":
					if (strlen($input_string) >= $length) $valid = 1;
					break;
				case "!=":
					if (strlen($input_string) != $length) $valid = 1;
					break;
			}
			echo "<br />"."check_alphanumeric_length: length:".strlen($input_string)."; compared length: $length; comparison operator:{$comparison_operator}";
			
		}		
	}
	
	echo "<br />"."check_alphanumeric_length: Validity of input string - $input_string:".$valid;
	if ($valid){
		return TRUE;
	}else{
		return FALSE;
	}
}

?>

Code: Select all

/*This does not work; Could any of you think of what is wrong here  or could you think of any logic that could bring down the number of lines not like the above*/
<?php
function check_alphanumeric_length($input_string, $length, $comparison_operator = ""){
	$valid = 0; //initially the input assumed to be invalid
	if(ereg("^[a-zA-Z0-9]*$", $input_string)){
		echo "<br />"."check_alphanumeric_length: Input - $input_string is a alphanumeric";
		if ($comparison_operator == ""){
			if (strlen($input_string) <= $length){
				$valid = 1;				
				echo "<br />"."check_alphanumeric_length: compared length:".strlen($input_string)."; comparison operator:<=; String is of valid length";
			}
		}else{
			//=======>this if block is where the problem arises<==========
			if (strlen($input_string) $comparison_operator $length){
				$valid = 1;				
				echo "<br />"."check_alphanumeric_length: compared length:".strlen($input_string)."; comparison operator:{$comparison_operator}; String is of valid length";
			}
		}		
	}
	
	echo "<br />"."check_alphanumeric_length: Validity of input string - $input_string:".$valid;
	if ($valid){
		return TRUE;
	}else{
		return FALSE;
	}
}

?>

Posted: Sat Nov 05, 2005 1:55 pm
by TJ
Use an eval() on strlen($input_string) $comparison_operator $length so it looks something like this (not tested):

Code: Select all

eval("\$result = ".strlen($input_string)." $comparision_operator $length");
if($result) {
 $valid = 1;
See PHP manual: eval