Jump to content


Photo

Advanced: Parsing String To Validate True/False


  • Please log in to reply
2 replies to this topic

#1 drewbee

drewbee
  • Members
  • PipPipPip
  • Advanced Member
  • 171 posts
  • LocationColumbus, Ohio

Posted 31 March 2006 - 12:30 AM

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)



<?
$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;
    }
}
?>

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!
With C you can shoot yourself in the leg. With C++ you can reuse the bullet.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 31 March 2006 - 12:54 AM

try

$string = "1 && 0 && 1 && 1";

$res = eval("return $string;");
if ($res)
    echo 'ok';
else
    echo 'no';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 drewbee

drewbee
  • Members
  • PipPipPip
  • Advanced Member
  • 171 posts
  • LocationColumbus, Ohio

Posted 31 March 2006 - 01:38 AM

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 :)
With C you can shoot yourself in the leg. With C++ you can reuse the bullet.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users