note 86917 added to function.eval

From: Date: Mon, 10 Nov 2008 17:26:07 +0000
Subject: note 86917 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-146358@lists.php.net to get a copy of this message
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! ---- Server IP: 209.41.74.194 Probable Submitter: 98.200.189.205 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/86917 Del: integrated -- https://master.php.net/note/delete/86917/integrated Del: useless -- https://master.php.net/note/delete/86917/useless Del: bad code -- https://master.php.net/note/delete/86917/bad+code Del: spam -- https://master.php.net/note/delete/86917/spam Del: non-english -- https://master.php.net/note/delete/86917/non-english Del: in docs -- https://master.php.net/note/delete/86917/in+docs Del: other reasons-- https://master.php.net/note/delete/86917 Reject -- https://master.php.net/note/reject/86917 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146358) next »