How to store code to be eval'd as array value?

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
ericm
Forum Newbie
Posts: 17
Joined: Tue Aug 26, 2008 8:33 am

How to store code to be eval'd as array value?

Post by ericm »

I'm missing something. I want to essentially keep an array of error message formats that will be used to set an error message based on parsed output. How do I store the output? I need to be able to store any valid PHP code as an array value and then parse it.
ericm
Forum Newbie
Posts: 17
Joined: Tue Aug 26, 2008 8:33 am

Re: How to store code to be eval'd as array value?

Post by ericm »

Anyone?

I get an "unexpected T_VARIABLE" even just doing

Code: Select all

$x = array( 1 => 'a string', 2 => $this->size)
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: How to store code to be eval'd as array value?

Post by marcth »

Not sure I understand what you are doing.

How about this?

Code: Select all

 
  $thisMember = 'whatever';
 
  $x = array( 1 => 'a string', 2 => '$thisMember');
  print '$x[2] = ' . eval($x[2] . ' and it's a ' . $x[1]);
 
ericm
Forum Newbie
Posts: 17
Joined: Tue Aug 26, 2008 8:33 am

Re: How to store code to be eval'd as array value?

Post by ericm »

marcth wrote:Not sure I understand what you are doing.

How about this?

Code: Select all

 
  $thisMember = 'whatever';
 
  $x = array( 1 => 'a string', 2 => '$thisMember');
  print '$x[2] = ' . eval($x[2] . ' and it's a ' . $x[1]);
 
 
Ok, I'll work with that. But for further clarification, I want to be able to store an error message that has place holders that are filled with the correct value when I use the error message.

For instance, I want one of my error messages to say "Invalid extension: ext (list of extensions). The bold would be place holders that are filled with the correct values on use: $this->ext and implode(', ', $this->allowedExts), respectively.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to store code to be eval'd as array value?

Post by Christopher »

I think you need to show some code. It is not clear what you are trying to do.
(#10850)
ericm
Forum Newbie
Posts: 17
Joined: Tue Aug 26, 2008 8:33 am

Re: How to store code to be eval'd as array value?

Post by ericm »

arborint wrote:I think you need to show some code. It is not clear what you are trying to do.
Here is a solution (albeit an ugly one, I think) for what I am trying to accomplish. I want to be able to have an array of error strings that may or may not contain variable placeholders. So in the example below, I'd want the error message to look like:

Extension .html not allowed (.txt, .doc, .pdf)

if that's what corresponding variables are set to at the time the error is printed.

Code: Select all

 
 
$x = array (
                     1 => 'return sprintf(\'Extension %s not allowed (%s)\', $this->file['ext'], implode(\', \', $this->exts));'
               );
 
print eval($x[1]);
 
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to store code to be eval'd as array value?

Post by Christopher »

I think sprintf() looks like a very good way to solve this problem. There are many ways to build strings with PHP, but if you are starting with a template string then sprintf() is a natural for this.
(#10850)
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: How to store code to be eval'd as array value?

Post by marcth »

ericm wrote: For instance, I want one of my error messages to say "Invalid extension: ext (list of extensions). The bold would be place holders that are filled with the correct values on use: $this->ext and implode(', ', $this->allowedExts), respectively.
PHP has a function called sprintf(). If you don't already know about it, you should definitely look that up. It's not suitable if you're dealing with multi-lingual content.

You could also look at the code behind my site, which makes use of the ActionResponse and UserMessages classes. The first is an internal messaging system that can take placeholders and the second is an external messaging system.
Post Reply