Просто пытаюсь выяснить правильный и безопасный способ выполнения математической операции, переданной как строка. В моем сценарии это значения, полученные из изображений EXIF.
После небольшого исследования я нашел два способа сделать это.
сначала, используя eval
:
function calculator1($str){
eval("\$str = $str;");
return $str;
}
второй, используя create_function
:
function calculator2($str){
$fn = create_function("", "return ({$str});" );
return $fn();
};
Оба примера требуют очистки строк, чтобы избежать выполнения вредоносного кода. Есть ли другой или более короткий способ сделать это?