Page 1 of 1
How to store code to be eval'd as array value?
Posted: Wed Sep 10, 2008 1:24 pm
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.
Re: How to store code to be eval'd as array value?
Posted: Thu Sep 11, 2008 10:05 am
by ericm
Anyone?
I get an "unexpected T_VARIABLE" even just doing
Code: Select all
$x = array( 1 => 'a string', 2 => $this->size)
Re: How to store code to be eval'd as array value?
Posted: Thu Sep 11, 2008 10:10 am
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]);
Re: How to store code to be eval'd as array value?
Posted: Thu Sep 11, 2008 10:35 am
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.
Re: How to store code to be eval'd as array value?
Posted: Thu Sep 11, 2008 10:39 am
by Christopher
I think you need to show some code. It is not clear what you are trying to do.
Re: How to store code to be eval'd as array value?
Posted: Thu Sep 11, 2008 4:14 pm
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]);
Re: How to store code to be eval'd as array value?
Posted: Thu Sep 11, 2008 4:38 pm
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.
Re: How to store code to be eval'd as array value?
Posted: Thu Sep 11, 2008 4:42 pm
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.