+ Reply to Thread
Results 1 to 9 of 9

Thread: PHP: Running code from string.

  1. #1
    borntyping's Avatar
    borntyping is offline x10Hosting Member borntyping is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    11

    Post PHP: Running code from string.

    I am working on a script that will take a text file and convert it to styled html.

    http://ziaix.x10hosting.com/projects...?showsource=on

    What I want to do is run php code from inside the file.

    The string will be something like this:
    PHP Code:
    <?php $str "<div>Some HTML <?php echo 'some php'; ?> More HTML </div>"?>
    (No dots or joiners there, the code will be in the string and could be complex.)

    Can anyone help?:dunno:
    Last edited by borntyping; 01-31-2010 at 03:51 AM.

  2. #2
    bioshock's Avatar
    bioshock is offline x10Hosting Member bioshock is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    27

    Re: PHP: Running code from string.

    you can do something like this using eval() function

    PHP Code:
    $str "echo 'some php';";
    eval(
    $str); 
    but cant embedded html that way and no php start and end tag;)

  3. #3
    borntyping's Avatar
    borntyping is offline x10Hosting Member borntyping is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    11

    Re: PHP: Running code from string.

    No, that doesn't work. Thanks anyway.

    I'm trying to use output buffering, but that doesn't work either. :eek4:
    PHP Code:
    <?php
        ob_start
    ();
        echo (
    $text);
        
    ob_end_flush();
    ?>

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP: Running code from string.

    I take it you're processing the file before you want PHP to evaluate it, and thus can't use include. You could try prepending '?>' to the string you pass to eval.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    borntyping's Avatar
    borntyping is offline x10Hosting Member borntyping is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    11

    Re: PHP: Running code from string.

    Quote Originally Posted by misson View Post
    I take it you're processing the file before you want PHP to evaluate it, and thus can't use include. You could try prepending '?>' to the string you pass to eval.
    Yes, I'm proccesing the file before showing it, and spliting it up into parts, leaving me with two strings. The first is broken up into an array to make the page details, and the second is the body of the page (where i want to include the php code).

    This is the new part of the code using eval.
    PHP Code:
    <?php
    function phpRun($code) {
        
    ob_start();
        
    $code stripslashes$code );
        
    $code html_entity_decode$code );
        
    $code "?>".$code."<?php ";
        eval(
    $code);
        
    $output ob_get_contents();
        
    ob_end_clean();
        return 
    $output;
    }

    $bbtext str_replace("}\n""}"$text);
    $bbtext str_replace("\&quot;"'"'$text);

    $match = array('#\{php\}(.*?)\{\/php\}#se');
    $replace = array( phpRun'$1' ) );

    $bbtext preg_replace($match$replace$bbtext);
    ?>
    This returns:
    HTML Code:
    Parse error:  syntax error, unexpected T_ECHO in /home/ziaix/public_html/projects/wrapper/wrap.php(64)  : regexp code on line 1
    
    Fatal error:  preg_replace() [<a  href='function.preg-replace'>function.preg-replace</a>]: Failed  evaluating code:   echo \&quot;this is PHP!\&quot;;  in /home/ziaix/public_html/projects/wrapper/wrap.php  on line 64
    Last edited by borntyping; 01-31-2010 at 07:29 AM.

  6. #6
    carrock is offline x10Hosting Member carrock is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    11

    Re: PHP: Running code from string.

    Quote Originally Posted by bioshock View Post
    you can do something like this using eval() function

    PHP Code:
    $str "echo 'some php';";
    eval(
    $str); 
    but cant embedded html that way and no php start and end tag;)
    No php start and end tag but I'm pretty sure something like


    PHP Code:
    // either a$ = file_get_contents(afile.txt);
    //or
    a$ = 'echo "'"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html><head><title>"'";
    // eg create $title_name with php
    echo  $title_name;
    echo 
    "</title>..... more html ";
    //etc' ;

    eval( a$); 
    would work ie use echo to create the html and use php without echo when required.

    There's a lot of fiddling required with ' and " etc to create a suitable a$ but I did something similar a while ago. The above code certainly :dunno: has errors but I can't check as I'm on Stoli :nuts:.

  7. #7
    xgreenberetx is offline x10Hosting Member xgreenberetx is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    57

    Re: PHP: Running code from string.

    what about fopen? you can log stuff and have it generate a html or php file.

  8. #8
    borntyping's Avatar
    borntyping is offline x10Hosting Member borntyping is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    11

    Re: PHP: Running code from string.

    Ok, I got it working.
    PHP Code:
    <?php
        $text 
    "?> ".$text;
        eval(
    $text);
    ?>
    Thanks to bioshock and misson, whose solutions I merged to get this code.

    Hard to belive it was so simple, really.
    Last edited by borntyping; 01-31-2010 at 11:38 AM.

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP: Running code from string.

    Just be certain your system is secure. There's a huge potential for injection vulnerabilities.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Hybrid's HTML Lessons
    By Hybrid in forum Tutorials
    Replies: 18
    Last Post: 11-28-2009, 02:12 PM
  2. Running PHP Scripts
    By newuser5 in forum Free Hosting
    Replies: 5
    Last Post: 09-24-2009, 01:06 PM
  3. how can i convert Php pages and code into asp pages and code
    By n4tec in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 01-10-2009, 09:40 PM
  4. checking PHP code
    By sclewin in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 10-24-2007, 02:13 AM
  5. php ad code problem
    By sourabhj in forum Free Hosting
    Replies: 7
    Last Post: 08-22-2006, 08:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers