Jump to content

Advanced: Parsing String To Validate True/False


drewbee

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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.