SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Free Geek computerages's Avatar
    Join Date
    Oct 2004
    Location
    /dev/null
    Posts
    1,071
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using eval to exec php code in html file

    Hi,

    Following is some dummy HTML file:

    HTML Code:
    <html> .... <?php echo 'HELLO!'; ?> ..... </html>
    I am reading it into a string using file_get_contents($file):

    PHP Code:
    $x file_get_contents($file); 
    Now, I use eval() to exec PHP code within $x string.

    PHP Code:
    eval('$x = "$x";'); 
    But, no luck. Output turns out to have php code as originally embedded in HTML file.

    Any ideas?

    Thanks.

  2. #2
    SitePoint Enthusiast premiumscripts's Avatar
    Join Date
    Aug 2009
    Location
    PremiumScripts.com
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no clue what you're trying to do here? Why would you want to use eval?

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As the parser is already reading when you execute eval, you need to stop it - prior to eval'ing the string.

    PHP Code:
    <?php
    $sHTML 
    '
    <html>
        <head>
            <title></title>
        </head>
        <body>
            <h3>Hello!</h3>
            The current time here is <?php echo date("r", time()); ?>.
        </body>
    </html>
    '
    ;

    echo eval(
    '?>' $sHTML);
    /*
    <html>
        <head>
            <title></title>
        </head>
        <body>
            <h3>Hello!</h3>
            The current time here is Wed, 05 Aug 2009 18:07:29 +0000.
        </body>
    </html>
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This has a similar functionality
    PHP Code:
    ob_start();
    include 
    $file;
    $x ob_get_clean(); 


Bookmarks

Posting Permissions

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