• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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.
0
MaybeItsJeremy
Asked:
MaybeItsJeremy
  • 2
1 Solution
 
quad341Commented:
using single quotes is your best bet, but you have to remember to either a) use double quotes in the content or b) escape the single quotes every time.  if this is dynamic, you could always use str_replace to replace ' with \' or something of the sort.  I found that for my purposes for doing this, escaping characters was well enough.  if you ever need to add in the value of a variable, you can end the quote, add a period, the variable, a period, a quote and continue. ie. 'content part 1 ='.$var.';\n more content'

your posted code misses an s in setting in the last declaration.  is there a reason you cannot do this?
0
 
alextr2003frCommented:
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
?>
0
 
alextr2003frCommented:
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'] .= '';
}
0
 
MaybeItsJeremyAuthor Commented:
I just needed to use escaped single quotes as quad341 said. Thanks :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now