如何用数学方法评估像“2-1"这样的字符串?产生“1"? [英] How to mathematically evaluate a string like "2-1" to produce "1"?

查看:26
本文介绍了如何用数学方法评估像“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.其他系统确实存在,并且在发布这个问题之前就已经存在,所以我很困惑为什么这里的每个答案都告诉您使用 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.

在我进入示例之前,获取我将使用的类的位置在 PHPClassesGitHub.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.

使用这样的类的原因是它包含并中缀到 postfix(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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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