Jump to content

Archived

This topic is now archived and is closed to further replies.

drewbee

Advanced: Parsing String To Validate True/False

Recommended Posts

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!

Share this post


Link to post
Share on other sites
try

[code]$string = "1 && 0 && 1 && 1";

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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.