How to store code to be eval'd as array value?
Moderator: General Moderators
How to store code to be eval'd as array value?
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?
Anyone?
I get an "unexpected T_VARIABLE" even just doing
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?
Not sure I understand what you are doing.
How about this?
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?
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.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]);
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.
- 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?
I think you need to show some code. It is not clear what you are trying to do.
(#10850)
Re: How to store code to be eval'd as array value?
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:arborint wrote:I think you need to show some code. It is not clear what you are trying to do.
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]);
- 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?
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)
Re: How to store code to be eval'd as array value?
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.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.
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.