eval 相关问题

eval是一个允许程序员从代码中的字符串变量执行以相同语言编写的任意代码的函数。

我可以使用 random 从某个范围中获取随机值。如何制作既可以执行相反操作又可以访问数学模块的数学评估器?

我想构建一个供私人使用的数学评估器(在进行评估时没有安全限制),它可以为我提供数学陈述的可能结果。假设这里的 random() 指的是 rang...

回答 1 投票 0

如何将 Shiny 模块内的 textInput 作为参数进行计算

我正在开发一个闪亮的应用程序,用作 gtsummary 包的界面。 我添加的功能之一是能够将 add_p() 等函数应用到生成的表,而且它...

回答 1 投票 0

如何使用修改后的窗口对象执行 eval 函数

我在代码中调用了 eval,并且引用了 iframe 的窗口对象。有什么方法可以执行 eval (或实现相同效果的方法),但将 window 对象设置为 i...

回答 1 投票 0

php 中 eval 的意外行为 < 7.4

以下代码: 以下代码: <?php function e($m, $n) { return eval( 'return new class() { // '. str_repeat('a', $m). ' public function test() { return "'. $n. '"; } };' ); } for ($i = 131; $i < 140; $i++) { echo $i. ': '. e($i, 1)->test(). ', '. e($i, 2)->test(). ', '. e($i, 1)->test(). ', '. e($i, 2)->test(). ', '. e($i + 1, 1)->test(). ', '. e($i + 1, 2)->test(). "\n"; } 当使用 php 7.4 或 8.* 运行时,它会打印 131: 1, 2, 1, 2, 1, 2 132: 1, 2, 1, 2, 1, 2 133: 1, 2, 1, 2, 1, 2 134: 1, 2, 1, 2, 1, 2 135: 1, 2, 1, 2, 1, 2 136: 1, 2, 1, 2, 1, 2 137: 1, 2, 1, 2, 1, 2 138: 1, 2, 1, 2, 1, 2 139: 1, 2, 1, 2, 1, 2 这是预期的。当使用 php 7.3 - 7.0 运行时,它会打印 131: 1, 2, 2, 2, 1, 1 132: 1, 1, 1, 1, 1, 2 133: 1, 2, 1, 2, 1, 2 134: 1, 2, 1, 2, 1, 2 135: 1, 2, 1, 2, 1, 1 136: 1, 1, 1, 1, 1, 1 137: 1, 1, 1, 1, 1, 1 138: 1, 1, 1, 1, 1, 1 139: 1, 1, 1, 1, 1, 1 这是非常出乎意料的。我还没有找到解决方法,但我真的需要一个! 最初似乎是评估代码开头的字符数相同,但当代码更复杂时,情况就不再是这样了。 请帮忙! 我不确定是什么导致了你的问题,但是如果可能的话,必须避免使用eval(),这里是可能的: <?php class MyTestClass { private $n; private $m; public function __construct($m, $n) { $this->m = str_repeat('a', $m); $this->n = $n; } public function test() { return $this->n; } } function evalClass($m, $n) { return new MyTestClass($m, $n); } for ($i = 131; $i < 140; $i++) { echo $i. ': '. evalClass($i, 1)->test(). ', '. evalClass($i, 2)->test(). ', '. evalClass($i, 1)->test(). ', '. evalClass($i, 2)->test(). ', '. evalClass($i + 1, 1)->test(). ', '. evalClass($i + 1, 2)->test(). "\n"; } 输出: 131: 1, 2, 1, 2, 1, 2 132: 1, 2, 1, 2, 1, 2 133: 1, 2, 1, 2, 1, 2 134: 1, 2, 1, 2, 1, 2 135: 1, 2, 1, 2, 1, 2 136: 1, 2, 1, 2, 1, 2 137: 1, 2, 1, 2, 1, 2 138: 1, 2, 1, 2, 1, 2 139: 1, 2, 1, 2, 1, 2

回答 1 投票 0

将 setTimeout 与字符串一起使用会触发不安全评估警报

以下是它在 Chrome 开发工具中的显示示例: setTimeout(console.log(`String`), 100); 字符串拒绝将字符串评估为 JavaScript,因为“unsafe-eval”不是允许的 sc 源...

回答 1 投票 0

使用索引数组向对象数组添加条目

我有以下场景:我有一个对象数组(TreeItems),其中可以包含嵌套数组。我还有另一个带有索引的数组。现在我想使用...来更改 TreeItems 数组

回答 1 投票 0

创建与现有实例相同的类的新实例,而不使用 eval()

我有一个未知类的实例sampleInstance,我需要创建同一类的第二个实例。这就是我现在正在做的事情: 我用sampleInstance.const查找类名...

回答 1 投票 0

如何在 bash 中通过 ref 将参数传递给脚本

请注意,我说的是通过引用将参数传递给脚本,而不是函数。我已经检查过这里和这里,他们讨论了使用函数传递参数。 我想要什么我...

回答 1 投票 0

将 eval() 与变量一起使用并出现语法错误

我正在尝试使用 while 循环查找文件中数字的平均值。 我使用 readline() 从文件中获取数字并将它们分配给一个变量,这会导致它们被读取为

回答 1 投票 0

非标准评估:对*编程*(不是交互式 R)有什么好处?

我不明白R中NSE(非标准评估)用于编程的成本效益。我明白为什么 NSE 对于交互式 R 可能有用,但对于编程——即编写可重用的脚本和

回答 1 投票 0

在 eval() 函数中的打印语句中不起作用

我正在使用Python进行一个项目,并且遇到了错误。 当我尝试运行此代码时: eval("print(\" \")"),它不起作用并给我一个错误:'SyntaxError: EOL ...

回答 1 投票 0

如何动态计算 C# 表达式?

我想做相当于: 对象结果 = Eval("1 + 3"); string now = Eval("System.DateTime.Now().ToString()") 作为字符串 按照 Biri 的链接,我得到了这个片段(修改为删除

回答 10 投票 0

将字符串列转换为公式

我正在尝试将列中的字符串转换为公式,并根据我指定的值输出结果。例如: 组合<- c("FE56", "FE58", "FE61", "YI6...

回答 2 投票 0

Playwright locator.evaluateAll 如何返回使用节点 forEach 填充的对象数组

更新见下文 我想获取所有 节点的属性和 类型或 类型的节点子节点的 innerText 简化版...

回答 1 投票 0

在 JavaScript 程序中重构动态加载的脚本以避免 eval

这可能是一个特定的问题(研究项目),但我正在寻找一种使该程序比目前“更安全”的好方法。 目标是接受任意

回答 1 投票 0

使用 eval 从字符串计算数学表达式

我想从字符串计算数学表达式。我已经读到解决这个问题的方法是使用 eval()。但是当我尝试运行以下代码时: 我想从字符串计算数学表达式。我已经读到解决这个问题的方法是使用 eval()。但是当我尝试运行以下代码时: <?php $ma ="2+10"; $p = eval($ma); print $p; ?> 它给了我以下错误: 解析错误:语法错误,意外的$end in C:\xampp\htdocs clipseWorkspaceWebDev\MandatoryHandinSite ester.php(4) :第 1 行的 eval() 代码 有人知道这个问题的解决方案吗 虽然我不建议为此使用eval(不是解决方案),但问题是eval需要完整的代码行,而不仅仅是片段。 $ma ="2+10"; $p = eval('return '.$ma.';'); print $p; 应该做你想做的事。 更好的解决方案是为您的数学表达式编写分词器/解析器。这是一个非常简单的基于正则表达式的示例: $ma = "2+10"; if(preg_match('/(\d+)(?:\s*)([\+\-\*\/])(?:\s*)(\d+)/', $ma, $matches) !== FALSE){ $operator = $matches[2]; switch($operator){ case '+': $p = $matches[1] + $matches[3]; break; case '-': $p = $matches[1] - $matches[3]; break; case '*': $p = $matches[1] * $matches[3]; break; case '/': $p = $matches[1] / $matches[3]; break; } echo $p; } 看看这个.. 我在会计系统中使用它,您可以在其中在金额输入字段中编写数学表达式.. 例子 $Cal = new Field_calculate(); $result = $Cal->calculate('5+7'); // 12 $result = $Cal->calculate('(5+9)*5'); // 70 $result = $Cal->calculate('(10.2+0.5*(2-0.4))*2+(2.1*4)'); // 30.4 代码 class Field_calculate { const PATTERN = '/(?:\-?\d+(?:\.?\d+)?[\+\-\*\/])+\-?\d+(?:\.?\d+)?/'; const PARENTHESIS_DEPTH = 10; public function calculate($input){ if(strpos($input, '+') != null || strpos($input, '-') != null || strpos($input, '/') != null || strpos($input, '*') != null){ // Remove white spaces and invalid math chars $input = str_replace(',', '.', $input); $input = preg_replace('[^0-9\.\+\-\*\/\(\)]', '', $input); // Calculate each of the parenthesis from the top $i = 0; while(strpos($input, '(') || strpos($input, ')')){ $input = preg_replace_callback('/\(([^\(\)]+)\)/', 'self::callback', $input); $i++; if($i > self::PARENTHESIS_DEPTH){ break; } } // Calculate the result if(preg_match(self::PATTERN, $input, $match)){ return $this->compute($match[0]); } // To handle the special case of expressions surrounded by global parenthesis like "(1+1)" if(is_numeric($input)){ return $input; } return 0; } return $input; } private function compute($input){ $compute = create_function('', 'return '.$input.';'); return 0 + $compute(); } private function callback($input){ if(is_numeric($input[1])){ return $input[1]; } elseif(preg_match(self::PATTERN, $input[1], $match)){ return $this->compute($match[0]); } return 0; } } 我最近创建了一个 PHP 包,它提供了一个 math_eval 辅助函数。它完全可以满足您的需求,而无需使用可能不安全的eval功能。 只需传入数学表达式的字符串版本,它就会返回结果。 $two = math_eval('1 + 1'); $three = math_eval('5 - 2'); $ten = math_eval('2 * 5'); $four = math_eval('8 / 2'); 你也可以传入变量,如果需要的话将被替换。 $ten = math_eval('a + b', ['a' => 7, 'b' => 3]); $fifteen = math_eval('x * y', ['x' => 3, 'y' => 5]); 链接:https://github.com/langleyfoxall/math_eval 当您无法控制字符串参数时,使用 eval 函数是非常危险的。 尝试 Matex 进行安全的数学公式计算。 解决了! <?php function evalmath($equation) { $result = 0; // sanitize imput $equation = preg_replace("/[^a-z0-9+\-.*\/()%]/","",$equation); // convert alphabet to $variabel $equation = preg_replace("/([a-z])+/i", "\$$0", $equation); // convert percentages to decimal $equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation); $equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation); $equation = preg_replace("/([0-9]{1})(%)/",".0\$1",$equation); $equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation); if ( $equation != "" ){ $result = @eval("return " . $equation . ";" ); } if ($result == null) { throw new Exception("Unable to calculate equation"); } echo $result; // return $equation; } $a = 2; $b = 3; $c = 5; $f1 = "a*b+c"; $f1 = str_replace("a", $a, $f1); $f1 = str_replace("b", $b, $f1); $f1 = str_replace("c", $c, $f1); evalmath($f1); /*if ( $equation != "" ){ $result = @eval("return " . $equation . ";" ); } if ($result == null) { throw new Exception("Unable to calculate equation"); } echo $result;*/ ?> 这种方法有两个主要缺点: 安全性,php脚本正在被eval函数评估。这不好, 特别是当用户想要注入恶意代码时。 复杂性 我创建了这个,检查一下:Formula Interpreter 它是如何工作的? 首先,使用公式及其参数创建FormulaInterpreter的实例 $formulaInterpreter = new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]); 用execute()方法解释公式。它将返回结果: echo $formulaInterpreter->execute(); 在一行中 echo (new FormulaInterpreter("x + y", ["x" => 10, "y" => 20]))->execute(); 例子 # Formula: speed = distance / time $speed = (new FormulaInterpreter("distance/time", ["distance" => 338, "time" => 5]))->execute() ; echo $speed; #Venezuela night overtime (ordinary_work_day in hours): (normal_salary * days_in_a_work_month)/ordinary_work_day $parameters = ["normal_salary" => 21000, "days_in_a_work_month" => 30, "ordinary_work_day" => 8]; $venezuelaLOTTTArt118NightOvertime = (new FormulaInterpreter("(normal_salary/days_in_a_work_month)/ordinary_work_day", $parameters))->execute(); echo $venezuelaLOTTTArt118NightOvertime; #cicle area $cicleArea = (new FormulaInterpreter("3.1416*(radio*radio)", ["radio" => 10]))->execute(); echo $cicleArea; 关于公式 它必须至少包含两个操作数和一个运算符。 Operands的名称可以是大写或小写。 到目前为止,数学函数如 sin、cos、pow……不包括在内。我正在努力将它们包括在内。 如果您的公式无效,您将收到如下错误消息:错误,您的公式(single_variable)无效。 参数的值必须是数字。 如果你愿意,你可以改进它! 在 eval 的危险和无限的计算可能性之间找到最佳平衡点我建议只检查数字、运算符和括号的输入: if (preg_match('/^[0-9\+\-\*\/\(\)\.]+$/', $mathString)) { $value = eval('return ' . $mathString . ';'); } else { throw new \Exception('Invalid calc() value: ' . $mathString); } 还是比较好用,比较省事。它可以处理任何基本的数学计算,如(10*(1+0,2)),这对于此处提到的大多数解决方案都是不可能的。 eval 将给定代码评估为 PHP。这意味着它将把给定的参数作为一段 PHP 代码执行。 要更正您的代码,请使用: $ma ="print (2+10);"; eval($ma); 使用评估函数 protected function getStringArthmeticOperation($value, $deduct) { if($value > 0){ $operator = '-'; }else{ $operator = '+'; } $mathStr = '$value $operator $deduct'; eval("\$mathStr = \"$mathStr\";"); $userAvailableUl = eval('return '.$mathStr.';'); return $userAvailableUl; } $this->getStringArthmeticOperation(3, 1); //2 eval'd 表达式应该以“;”结尾 试试这个: $ma ="2+10;"; $p = eval($ma); print $p; 顺便说一句,这超出了范围,但“eval”函数不会返回表达式的值。 eval('2+10') 不会返回 12。 如果你想让它返回 12,你应该 eval('return 2+10;');

回答 10 投票 0

单击操作按钮时运行特定代码块

我正在尝试在 R Markdown 中创建一个 flexdashboard,它在单击操作按钮时运行特定的代码块。 IE。在仪表板的开头,会出现一个带有单选按钮的问卷,w ...

回答 0 投票 0

使用文本文件和 eval() 调用函数

我的练习是调用 hack 函数,只需返回“你被黑了!”在控制台上,通过使用 hack3.txt 文件而不修改代码,有什么想法吗?下面的代码: 导入操作系统 来自

回答 1 投票 0

如何在 Google 表格的指定单元格中评估存储的间接公式

我已经阅读了 Stack Overflow 上的所有内容,因为无法找到答案。关于这个主题的帖子也有好几年了,所以我希望有更新。 问题:你如何引用一个...

回答 1 投票 0

使用 model.eval() 导致不正确的预测

我已经通过 pytorch 训练了一个模型,并且该模型在训练期间在训练、验证和测试集上表现良好,但是当我读取保存的模型参数以在那里可视化测试集时...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.