Jump to content

Simple eval question


x1nick

Recommended Posts

Basically I am making a class which displays a set of data.

Now sometimes the data returned from the database will be a unix time stamp so this needs to be displayed in a correct form.

For this I use str_replace

 

$replace - below this will contain a way of converting the data

Replace is defined by:

$replace = 'date(\'d-m-y\',$row_data[\'lastupdate\'])';

 

$row.= str_replace('{data}',eval($replace),'<td>{data}</td>');

 

This just gives me

Parse error: syntax error, unexpected $end in C:\xampp\htdocs\z\src\lib\lib.table.php(348) : eval()'d code on line 1

 

Is this possible at all?

Link to comment
Share on other sites

I agree with PFMaBiSmAd, but you can replace {data} with PHP code and then later eval that:

$replace = '<?php echo date(\'d-m-y\', $row_data[\'lastupdate\']); ?>';
$row .= str_replace('{data}', $replace, '<td>{data}</td>');
// later
eval($row);

 

Or you can do it the normal way and do:

$replace = date('d-m-y', $row_data['lastupdate']);
$row .= str_replace('{data}', $replace, '<td>{data}</td>');

 

Or if you insist on doing it your way, use return:

$replace = 'return date(\'d-m-y\',$row_data[\'lastupdate\']);';
$row .= str_replace('{data}', eval($replace),'<td>{data}</td>');

 

But, as stated, this is probably not the best way to do it, though I have in dynamic templating where templates must be precompiled.

Link to comment
Share on other sites

Thanks

$replace = 'return date(\'d-m-y\',$row_data[\'lastupdate\']);';

worked perfectly.

 

$replace is set outside the class, the first suggested way would probably be best, but due to the way the whole system has been made, it wouldn't be easy to implment for the sake of this

 

Thanks again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.