PHP: Running code from string.

borntyping

New Member
Messages
11
Reaction score
0
Points
0
I am working on a script that will take a text file and convert it to styled html.

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

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

The string will be something like this:
PHP:
<?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:

bioshock

New Member
Messages
27
Reaction score
0
Points
0
you can do something like this using eval() function

PHP:
$str = "echo 'some php';";
eval($str);

but cant embedded html that way and no php start and end tag;)
 

borntyping

New Member
Messages
11
Reaction score
0
Points
0
No, that doesn't work. Thanks anyway.

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

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.
 

borntyping

New Member
Messages
11
Reaction score
0
Points
0
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:
<?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:
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:

carrock

New Member
Messages
11
Reaction score
0
Points
1
you can do something like this using eval() function

PHP:
$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:
// 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:.
 

borntyping

New Member
Messages
11
Reaction score
0
Points
0
Ok, I got it working.
PHP:
<?php
    $text = "?> ".$text;
    eval($text);
?>

Thanks to bioshock and misson, whose solutions I merged to get this code. :biggrin:

Hard to belive it was so simple, really.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Just be certain your system is secure. There's a huge potential for injection vulnerabilities.
 
Top