note 86917 deleted from function.eval by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 19:51:04 +0000 |
| Subject: | note 86917 deleted from function.eval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155279@lists.php.net to get a copy of this message | ||
Note Submitter: alpha_centurion at hotmail dot com
----
Re: the comment from
Mark dot Sheppard at disney dot com
30-Oct-2008 05:13
You can stop execution of an evaluated statement at any point within the eval() by using a return
statement - this does not halt the execution of the main script (like exit() does).
If you want to stop an evaluated string from using the exit() statement, just do a find and replace
on the string first, e.g.:
<?php
// Get the code to eval() however you like...
$original = 'echo "hi"; exit();';
// Replace all occurences of exit() with something safer
$pattern = '/\\bexit[\\s]*(\\([^\\)]*\\))?[\\s]*[;]?/i';
$no_exit = preg_replace($pattern,'return true;',$original);
// Output samples of strings (if you're interested seeing results)
#echo '<pre>' . $original . '</pre>' . "\n\n";
#echo '<pre>' . $no_exit . '</pre>' . "\n\n";
// Run the 'fixed' version of the statement
$result = eval($no_exit);
// This will only fire if you make it past
// the eval statement
echo '<p>Made it past the eval!</p>';
?>
Note: Because eval() will return the value of a return statement, you can also give the return
statement a value which you can test for. In this case I've just set it to true; you could do
what you like. If you felt like getting fancy, you could even enumerate each exit with it's
occurrence in the script as a string return to know where it had exited.
The regex is good enough for most variants I've seen for the use of an exit() statement, but I
couldn't say it's bullet proof!