drewbee Posted March 31, 2006 Share Posted March 31, 2006 Hello everyone, I am having quite a bit of an issue going on here. To keep things simple, after something is run I end up with a string (Dynamically generated) that can only contain four charecters (&&,||,0,1), but an unlimited total amount of these.So for instance, $string = "0 && 1 && 1 || 1";Now, what I want to do is validate this string where 0 = false, and 1 true.The String above would come out to "FALSE && TRUE && TRUE OR FALSE";if contained within a statement: if (FALSE && TRUE && TRUE OR FALSE){ // Its good}else{ // Bad}It would return FALSE (ELSE) due to the first two statements linked by &&, one is false, thus going to the "bad" end of the statement.So, how in the world do I do this? I tried playing around with eval a little bit, but couldn't get it to work like I wanted. If you know how to do this, please let me know, below is how I tried to do this and am really off i think ( it doesn't work anyways)[code]<?$string = "1 && 0 && 1 && 1";retrictionDecide($string);function restrictionDecide($string){ $string = str_replace(array("0","1"),array(restrictionReturn(0),restrictionReturn(1)),$string); $actual = "if (\$string) { return true; } else { return false; }"; if (eval($actual)) { echo "no ok"; } else { echo "ok"; } }function restrictionReturn($boolean){ if ($boolean == "1") { return false; } elseif ($boolean == "0") { return true; }}?>[/code]This above, however, seems to return "ok" no matter what, which obviously means it isn't processing it correctly, due to my own logic, of course :)If anyone could have any advice and previous known experience with this, it would be an absolute tremendous help if this could be situated! Quote Link to comment Share on other sites More sharing options...
Barand Posted March 31, 2006 Share Posted March 31, 2006 try[code]$string = "1 && 0 && 1 && 1";$res = eval("return $string;");if ($res) echo 'ok';else echo 'no';[/code] Quote Link to comment Share on other sites More sharing options...
drewbee Posted March 31, 2006 Author Share Posted March 31, 2006 Fasinating, who would have known that it could evaluate 0's and 1's as they were, lol. You my friend, save my life more then you know it :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.