running script from textfile array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

running script from textfile array

Post 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?
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post 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 :)
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post 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 ;)
Post Reply