dynamically passing comparison operator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

dynamically passing comparison operator

Post 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;
	}
}

?>
TJ
Forum Newbie
Posts: 20
Joined: Thu Nov 03, 2005 10:22 pm
Location: Nottingham, UK

Post 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
Post Reply