问题描述
我只是想知道 PHP 是否有一个函数可以接受像 2-1
这样的字符串并产生它的算术结果?
I was just wondering if PHP has a function that can take a string like 2-1
and produce the arithmetic result of it?
或者我必须手动使用 explode()
来获取算术运算符的左右值吗?
Or will I have to do this manually with explode()
to get the values left and right of the arithmetic operator?
推荐答案
我知道这个问题很老了,但是我昨晚在搜索不太相关的东西时遇到了它,这里的每个答案都很糟糕.不仅糟糕,非常糟糕.我在这里给出的示例将来自我在 2005 年创建的一个类,并且因为这个问题而在过去的几个小时内更新了 PHP5.其他系统确实存在,并且在这个问题发布之前就已经存在,所以我很困惑为什么这里的每个答案都告诉你使用 eval
,当 PHP 的警告是:
I know this question is old, but I came across it last night while searching for something that wasn't quite related, and every single answer here is bad. Not just bad, very bad. The examples I give here will be from a class that I created back in 2005 and spent the past few hours updating for PHP5 because of this question. Other systems do exist, and were around before this question was posted, so it baffles me why every answer here tells you to use eval
, when the caution from PHP is:
eval() 语言结构非常危险,因为它允许执行任意 PHP 代码.因此不鼓励使用它.如果您已仔细验证除了使用此构造之外别无选择,请特别注意不要将任何用户提供的数据传入其中,而无需事先正确验证.
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
在我进入示例之前,获取我将使用的类的位置在 PHPClasses 或 GitHub.eos.class.php
和 stack.class.php
都是必需的,但可以合并到同一个文件中.
Before I jump in to the example, the places to get the class I will be using is on either PHPClasses or GitHub. Both the eos.class.php
and stack.class.php
are required, but can be combined in to the same file.
使用这样的类的原因是它包含和后缀(RPN)解析器的中缀,然后是 RPN 求解器.有了这些,您就不必使用 eval
函数并让您的系统面临漏洞.一旦你有了这些类,下面的代码就可以解决一个简单(更复杂)的方程,比如你的 2-1
例子.
The reason for using a class like this is that it includes and infix to postfix(RPN) parser, and then an RPN Solver. With these, you never have to use the eval
function and open your system up to vulnerabilities. Once you have the classes, the following code is all that is needed to solve a simple (to more complex) equation such as your 2-1
example.
require_once "eos.class.php";
$equation = "2-1";
$eq = new eqEOS();
$result = $eq->solveIF($equation);
就是这样!您可以对大多数方程使用这样的解析器,无论多么复杂和嵌套,都无需求助于邪恶的 eval
".
That's it! You can use a parser like this for most equations, however complicated and nested without ever having to resort to the 'evil eval
'.
因为我真的不希望这只是为了让我的班级参与其中,所以这里有一些其他选项.我只熟悉我自己的,因为我已经使用了 8 年.^^
Because I really don't want this only only to have my class in it, here are some other options. I am just familiar with my own since I've been using it for 8 years. ^^
Wolfram|Alpha API
圣人
一个相当糟糕的解析器
phpdicecalc
不太清楚我之前发现的其他人发生了什么 - 之前在 GitHub 上也遇到过另一个,不幸的是我没有为它添加书签,但它与包含解析器的大型浮动操作有关.
Not quite sure what happened to others that I had found previously - came across another one on GitHub before as well, unfortunately I didn't bookmark it, but it was related to large float operations that included a parser as well.
无论如何,我想确保在此处使用 PHP 求解方程的答案不会将所有未来的搜索者指向 eval
,因为它位于谷歌搜索的顶部.^^
Anyways, I wanted to make sure an answer to solving equations in PHP on here wasn't pointing all future searchers to eval
as this was at the top of a google search. ^^
这篇关于如何在数学上评估像“2-1"这样的字符串?产生“1"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!