Page 1 of 1

running script from textfile array

Posted: Sat May 04, 2002 12:00 pm
by lc
Ok this is it in a nutshell.

I'm using a nice bit of script doing this.

$base = explode("|", $file[$arr]);

But now the thing is... I'd like to check if a certain bit resulting from this is script... say $base[2] .. is script or text

And I'd like to run this bit as php script in stead of just printing it in a nice table.
So I got to check if the first 5 characters of $base[2] are <?php and if so.. then run it... I haven't been able to figure it out yet.

Or is it possible to just enter a code in the middle of any textbit that you retrieve from file that will be run? Or have a bit of script do that for you?

Posted: Sat May 04, 2002 12:15 pm
by samscripts
Hi, the function you need to use is eval().

Example:

Code: Select all

<?php

$base = array("Hello", "<br>", "<?php echo 'world';?>", "<br>","is this what you wanted?");

for( $i = 0; $i < count($base); $i++)&#123;
	if( substr($base&#1111;$i], 0, 5) == '<?php')&#123;
		$code = substr($base&#1111;$i], 5, strlen($base&#1111;$i])-7);
		eval( $code);
	&#125;else&#123;
		echo $base&#1111;$i];
	&#125;
&#125;

?>
It will execute the string passed to it as php code. Notice though that you shouldn't have the <?php...?> tags at the beginning and end of the code to be eval'd, although it is ok to jump in and out of php withing the eval'd stuff.

ie: This will not work:

Code: Select all

$code = "<?php echo 'hello world';?>";
eval ( $code);
but this will:

Code: Select all

$code = "echo 'hello world';?><hr><b><?php echo 'Understand?';?>";
eval ($code);
Hope this helps you out :)

Posted: Sun May 05, 2002 10:25 am
by lc
Yup that helped all I really need is the bottom line... I'll just format all like that and be done with it. thx ;)