如何在数学上评估像"2-1"之类的字符串.产生"1"? [英] How to mathematically evaluate a string like "2-1" to produce "1"?

查看:50
本文介绍了如何在数学上评估像"2-1"之类的字符串.产生"1"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道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进行了更新.确实存在其他系统,并且在此问题发布之前就已经存在,因此让我感到困惑的是,为什么这里的每个答案都告诉您使用

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.

在进入示例之前,获取要使用的类的位置位于 GitHub . eos.class.phpstack.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);

就是这样!您可以对大多数方程使用这样的解析器,无论它多么复杂和嵌套,都无需诉诸'evil 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

Wolfram|Alpha API
Sage
A fairly bad parser
phpdicecalc

不太确定我之前发现的其他人发生了什么-之前在GitHub上也遇到了另一个人,不幸的是我没有将其添加为书签,但这与包含解析器的大型float操作有关.

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,因为这是Google搜索的顶部. ^^

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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆