Re: how to run php code within string?

Ju Hui wrote:

thanks all your reply.
But the answer isn't what I want.

I know function eval(), it will execute the string pass to the function
as php script.

but my requirement is .

$a is a string, it will retrive from db.
I want to insert some php script to the $a, like {php}{/php} in Smarty
of PHP. The code with special tag will be processed as php script.
$a="result,<? if ($b==1) echo \"b=1\" ?>";
print $a
I want to get result,b=1.

thanks .


Just make sure you make the boundaries clear of the PHP code in the string
you store in the database.
$myStr = "result,**PHP**if ($b==1) echo \"b=1\"**PHP** testing.";
Now if you retrieve that string from DB, you can get the parts you want to
execute using explode("**PHP**",$myStr) and a little coding.

Of course, be sure that the seperatorstring (**PHP** in this example) cannot
be used elsewhere, or this will fail.

I must warn you, like others did, that you should try to avoid such design.

If the content of the executable PHP-code is coming from users, don't trust
it. I can contain anything, and you do NOT want to eval that. Beware.

A sidenote:
I have been coding PHP for years nonstop, and I only needed eval once.
I got so paranoid that I needed 2 days of additional coding and testing to
be sure it was safe.
My point: You probably do not need eval().

Erwin Moller

Relevant Pages

  • Re: Attempt to de-mystify AJAX
    ... created and populated by the PHP script. ... string is initialized in line 1 to the opening select tag. ...
  • Re: [PHP] Help with an error...
    ... And it is possible to have commands in the middle of a string by using concatenation. ... use that as the string for the mail function. ... Micah Gersten ... I am currently working on a php script that will be called by cron. ...
  • Re: Bug in imagettftext?
    ... encoding my PHP script as UTF-8 and then outputting a string that ... has been utf8_encode'ed with image imagettftext(), ... Was the string *previously* in UTF-8 before you passed it to ...
  • Re: Monitor Web & MySQL Services ??
    ... Is it possible to use a PHP script to detect if a web site is up and ... 'Connection Error' is shown on the web page then the site is down. ... or the 'connection error' string or MySQL errors ...
  • Re: [PHP] Help with an error...
    ... He had code blocks in the middle of a string. ... onShore Networks ... Ted Wood wrote: ... I am currently working on a php script that will be called by cron. ...