The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 8 of 8

Thread: Eval on edited string from database

  1. #1
    218
    posts
    Keyboard weilding drunk

    Eval on edited string from database

    Hey all,
    I've been working on this little script to replace values in a stored equation (database) with actual numbers and then running the equation. But all I keep getting is just the script echoing out the string instead of evaluating it.

    I know what I'm going to post is kinda long and the script isn't complete yet. It's just a proof of concept before I move forward with a larger portion of the project. any help would be much appreciated

    PHP Code:
    $eq 10*p1.str p1.dex p1.chr;

    replaceHoldersWithVals(createStatArr(getHolders($eq)),$eq);
        
        function 
    getHolders($str)
        {
            
    $statArr = array();
            
    $pos 0;
            
    $i 0;
            
    $len strlen($str);
            while(
    $i != strlen($str))
            {
                if(
    stripos($str,"p1",$pos))
                {
                    
    $pos stripos($str,"p1",$pos);
                    
    array_push($statArr,substr($str,$pos+3,3));
                    
    $pos += 6;
                }else{
                    
    $i $len
                }
            }
            return 
    $statArr;
        }
        
        function 
    createStatArr($statArr)
        {
            for(
    $i 0$i count($statArr);$i++)
            {
                switch(
    $statArr[$i])
                {
                    case 
    "str":
                        {
                            
    $statArr[$i] = array("str"=>10);
                            break;
                        }
                    case 
    "dex":
                        {
                            
    $statArr[$i] = array("dex"=>7);
                            break;
                        }
                    case 
    "chr":
                        {
                            
    $statArr[$i] = array("chr"=>2);
                            break;
                        }
                }
            }
            
            return 
    $statArr;
        }
        
        function 
    replaceHoldersWithVals($statArr$str)
        {
            
    $pos 0;
            
    $len strlen($str);
            for(
    $i 0$i count($statArr);$i++)
            {
                
    $str str_ireplace("p1.".key($statArr[$i]),$statArr[$i][key($statArr[$i])],$str);
            }
            eval(
    "\$e = \"$str\";");
            echo 
    $e;
        } 
    For developers, by developers - Raising the bar for PBBGs:
    http://openbracket.ca


  2. #2
    1,627
    posts
    hugeExplosions = true;
    You need to eval the $str inside the eval I think

    Or you could use
    PHP Code:
    $e = eval($str); 
    Though I'm probably wrong
    Last edited by Charleh; September 20th, 2009 at 03:31 PM.
    MS Paint FTW!


  3. #3
    218
    posts
    Keyboard weilding drunk
    Hey Charleh,
    Thanks for the reply.
    I've tired :

    PHP Code:
    $e = eval($str); 
    and I get this error : Parse error: parse error in C:\wamp\www\test.php(76) : eval()'d code on line 1

    pretty lost as what to do lol
    For developers, by developers - Raising the bar for PBBGs:
    http://openbracket.ca


  4. #4

    Example string

    It'll just be an invalid PHP string that you are trying to eval. Do you have an example string that you're trying to evaluate?

    Cheers
    Tristan


    Quote Originally Posted by bardic View Post
    Hey Charleh,
    Thanks for the reply.
    I've tired :

    PHP Code:
    $e = eval($str); 
    and I get this error : Parse error: parse error in C:\wamp\www\test.php(76) : eval()'d code on line 1

    pretty lost as what to do lol

  5. #5
    1,627
    posts
    hugeExplosions = true;
    Yeah it looks like seeing as the eval()d PHP threw a parse error that there's an error in the PHP contained in $str!

    echo $str to the output stream and see what the outputted string looks like
    MS Paint FTW!


  6. #6
    218
    posts
    Keyboard weilding drunk
    This is the string i get back : 10*5 /4 + 5 / 2
    For developers, by developers - Raising the bar for PBBGs:
    http://openbracket.ca


  7. #7
    218
    posts
    Keyboard weilding drunk
    Bwhahaha! I got it. bloody thing giving me trouble the last few days.
    The problem was how I was eval()'ing the string. I'm not 100% to the reason why it works or broke but I can show ya the change I made.

    I change this :
    PHP Code:
     eval("\$e = \"$str\";"); 
    to this:
    PHP Code:
    eval("\$e = $str;"); 
    Now if anyone can explain why the difference that would be awesome ^_^
    For developers, by developers - Raising the bar for PBBGs:
    http://openbracket.ca


  8. #8
    1,627
    posts
    hugeExplosions = true;
    Well, why don't you write the result out in text and see what code you get?

    first one:

    PHP Code:
    $e "10*5 /4 + 5 / 2"
    second one

    PHP Code:
    $e 10*/2
    Do you get it yet? The first one you are basically assigning $e to the string value of "10*5 /4 + 5 / 2" as you have enclosed it in quotes. The second one is evaluated as if it was typed as I have typed it (numerically/mathematically).
    MS Paint FTW!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012