Eval

Discussion in 'PHP' started by crazyryan, Aug 1, 2007.

  1. #1
    Hey,
    Basically, in my code below, I am echoing $code, within $code is:
    <img src=\"". $file . "\" />
    (stored in the database)

    I want to be able to define $file, as I have done earlier in my code. However when I echo out $code, it shows <img src=\"". $file . "\" /> and not <img src=\"WHAT I DEFINE AS $FILE\" />

    Someone told me to use eval? How would I do it?
     
    crazyryan, Aug 1, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    $str = '<img src=\"". $file . "\" />';
    $file = 'image.jpg';
    
    eval('$new_str = "' . $str . '";');
    
    echo $new_str;
    
    PHP:
     
    nico_swd, Aug 1, 2007 IP
    crazyryan likes this.
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    No! :p Do not use eval() unless it is really needed. I think this was just a syntax issue.

    crazyryan, can you post some more of the code, as I'm sure a non-eval() solution can be found.
     
    krt, Aug 2, 2007 IP
  4. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #4
    I tried:
    
    $display = mysql_query("SELECT * FROM `types` WHERE `type` = '" . $type . "'");
    $codetype = mysql_fetch_array($display);
    $str = $codetype['code'];
    eval('$code = "' . $str . '";');
    echo $code;
    PHP:
    But got:

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/phpmedia/public_html/arcade/game.php(28) : eval()'d code on line 1

    Parse error: syntax error, unexpected '"' in /home/phpmedia/public_html/arcade/game.php(28) : eval()'d code on line 1

    EDIT: This was due to the row in the database, I have fixed it now.

    @krt: I really can't see any other way other than doing an if statement for every single file type.

    EDIT2: Forgot to say, huge thanks :)
     
    crazyryan, Aug 2, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    // str_replace() because we only want to escape double quotes.
    eval('$code = "' . str_replace('"', '\"', $str) . '";');
    
    PHP:
     
    nico_swd, Aug 2, 2007 IP
  6. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #6
    I'm having a bit of trouble with the rows in the database, for one of the codes I have:
    <object type=\"application/x-shockwave-flash\" data=\"" . $file . "\" width=\"" . $width ."\" height=\"" . $ height . "\">
    <param name=\"movie\" value=\"" . $file . "\" />
    </object>

    But I'm getting:

    Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /home/phpmedia/public_html/arcade/game.php(28) : eval()'d code on line 1


    I'm not using nico's code atm, when I did try the variables weren't being set.
     
    crazyryan, Aug 2, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    
    =\"" . $ height . "\">
    
    Code (markup):
    There's a space between the dollar sign and variable name.
     
    nico_swd, Aug 2, 2007 IP
  8. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #8
    Woops, thanks again, nico_swd to the rescue :D
     
    crazyryan, Aug 2, 2007 IP
  9. nagasharmi

    nagasharmi Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    <img src="foldername/<? echo $res['image']?> " >
     
    nagasharmi, Aug 2, 2007 IP