x1nick Posted October 20, 2010 Share Posted October 20, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/216412-simple-eval-question/ Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 I'm not sure it's going to work but the parse error is because your eval code is not proper PHP code. It needs a ; just like any other time. Quote Link to comment https://forums.phpfreaks.com/topic/216412-simple-eval-question/#findComment-1124622 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 For what you are doing, there's absolutely no reason to be using eval(). Quote Link to comment https://forums.phpfreaks.com/topic/216412-simple-eval-question/#findComment-1124623 Share on other sites More sharing options...
x1nick Posted October 20, 2010 Author Share Posted October 20, 2010 So theres no simple way for me to replace {data} with some php code I thought about putting some eval code in a output buffer, but this seems a little excessive and guessing it would use quite a bit of system resources Quote Link to comment https://forums.phpfreaks.com/topic/216412-simple-eval-question/#findComment-1124625 Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216412-simple-eval-question/#findComment-1124627 Share on other sites More sharing options...
x1nick Posted October 20, 2010 Author Share Posted October 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216412-simple-eval-question/#findComment-1124632 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.