Link to home
Start Free TrialLog in
Avatar of MaybeItsJeremy
MaybeItsJeremy

asked on

Using non-executing (plain-text) PHP code in a variable

I'd like to use non-parsing/executing PHP code in a variable, as I will be writing the content to a file where it will actually be parsed at a later time.



$setting['content'] .='
<?php
if ( ! defined( 'INCLUDED' ) )
{
      print "<h1>Invalid access</h1>";
      exit();
}
else{
$variable =\'';

$setting['content'] .= 'content here';
$etting['content'] .= '\';
}
?>';





In the end, I want $settings['content'] to have a value of something like:

<?php
if ( ! defined( 'INCLUDED' ) )
{
      print "<h1>Invalid access</h1>";
      exit();
}
else{
$variable ='content here';
}
?>


I need most of everything to stay intact the way I have it, using .= becuase it's going to be a class system and the only thing that will change for the different files is the "content here" area. It's important to keep in mind, I am NOT trying to parse any of the value of the variable. I just want to collect all of the values so I can write that to a file that will actually be parsed as normal.
ASKER CERTIFIED SOLUTION
Avatar of quad341
quad341

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alextr2003fr
alextr2003fr

you can use eval command to evaluate some string as php code later, for example in your case do :
<?php
$setting['content'] .='
if ( ! defined( \'INCLUDED\' ) )
{
     print "<h1>Invalid access</h1>";
     exit();
}
else{
$variable =\'\';

$setting[\'content\'] .= \'content here\';
$etting[\'content\'] .= \'\';
}'; //storing content in your $setting array


eval($setting['content']); //evaluating
?>
this also works when writing to a file
<?php
$setting['content'] .='
if ( ! defined( \'INCLUDED\' ) )
{
     print "<h1>Invalid access</h1>";
     exit();
}
else{
$variable =\'\';

$setting[\'content\'] .= \'content here\';
$etting[\'content\'] .= \'\';
}'; //storing content in your $setting array

$file = fopen('textfile.txt', 'w');
fwrite($file, $setting['content']);
fclose($file);

?>
open your textfile.txt and your result will be :
if ( ! defined( 'INCLUDED' ) )
{
     print "<h1>Invalid access</h1>";
     exit();
}
else{
$variable ='';

$setting['content'] .= 'content here';
$etting['content'] .= '';
}
Avatar of MaybeItsJeremy

ASKER

I just needed to use escaped single quotes as quad341 said. Thanks :)