Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given a logical expression ( for eg. ((A AND B) OR C) ) , How can one extract individual elements like A, B, C in php. The logical expression can be anything , comprised of AND, OR and BRACKETS.
Thanks in advance.
Posted
Comments
Akinmade Bond 25-Sep-13 5:27am    
Can you please be more explicit? You want to extract the variables from a logical expression?
mg9893 25-Sep-13 11:52am    
Yes I want to extract variables from any logical expression given by the user.
Akinmade Bond 25-Sep-13 12:39pm    
The user is supposed to write the logical expression?
mg9893 25-Sep-13 12:48pm    
Yes,Its a part of a project. I am taking a logical expression comprised of AND, OR , BRACKETS and VARIABLES from the user as an input , then after extracting variables , I am working on those individual variables.
Akinmade Bond 25-Sep-13 12:58pm    
You could filter the user input, stripping out logical variables and special characters.

1 solution

Try this, you should be able to improve on it.

PHP
if (!empty($_POST)) {	
	//The list of words to remove
	$keywords = array( 'IF', 'THEN', 'ELSE', 'ENDIF', 'AND', 'OR', 'XOR', '&&', '&', 'IS', 'NOT', 'WHERE' );
	//The expression the user entered.
	$logic =  explode(' ', htmlentities ( strtoupper( trim($_POST['logic']) ) )  ) ;
	//Remove all keywords and print them to screen.
	foreach (array_diff( $logic, $keywords ) as $key => $value ){
		echo $value .'<br />';
	}
}

?>

<form method="post">
<textarea name="logic"> </textarea>
<input type="submit" />
</form>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900