<bdo id='2LiTj'></bdo><ul id='2LiTj'></ul>
  • <small id='2LiTj'></small><noframes id='2LiTj'>

  • <tfoot id='2LiTj'></tfoot>

        <legend id='2LiTj'><style id='2LiTj'><dir id='2LiTj'><q id='2LiTj'></q></dir></style></legend>
        <i id='2LiTj'><tr id='2LiTj'><dt id='2LiTj'><q id='2LiTj'><span id='2LiTj'><b id='2LiTj'><form id='2LiTj'><ins id='2LiTj'></ins><ul id='2LiTj'></ul><sub id='2LiTj'></sub></form><legend id='2LiTj'></legend><bdo id='2LiTj'><pre id='2LiTj'><center id='2LiTj'></center></pre></bdo></b><th id='2LiTj'></th></span></q></dt></tr></i><div id='2LiTj'><tfoot id='2LiTj'></tfoot><dl id='2LiTj'><fieldset id='2LiTj'></fieldset></dl></div>

        如何在数学上评估像“2-1"这样的字符串?产生“1"?

        时间:2024-08-15
        • <legend id='5yFbe'><style id='5yFbe'><dir id='5yFbe'><q id='5yFbe'></q></dir></style></legend>

          <small id='5yFbe'></small><noframes id='5yFbe'>

          1. <i id='5yFbe'><tr id='5yFbe'><dt id='5yFbe'><q id='5yFbe'><span id='5yFbe'><b id='5yFbe'><form id='5yFbe'><ins id='5yFbe'></ins><ul id='5yFbe'></ul><sub id='5yFbe'></sub></form><legend id='5yFbe'></legend><bdo id='5yFbe'><pre id='5yFbe'><center id='5yFbe'></center></pre></bdo></b><th id='5yFbe'></th></span></q></dt></tr></i><div id='5yFbe'><tfoot id='5yFbe'></tfoot><dl id='5yFbe'><fieldset id='5yFbe'></fieldset></dl></div>
              <bdo id='5yFbe'></bdo><ul id='5yFbe'></ul>
              <tfoot id='5yFbe'></tfoot>

                    <tbody id='5yFbe'></tbody>

                  本文介绍了如何在数学上评估像“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.

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

                  就是这样!您可以对大多数方程使用这样的解析器,无论多么复杂和嵌套,都无需求助于邪恶的 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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 PHP 认为 0 等于字符串? 下一篇:在 PHP 中显示带序号后缀的数字

                  相关文章

                  <legend id='LLVIa'><style id='LLVIa'><dir id='LLVIa'><q id='LLVIa'></q></dir></style></legend>

                    <small id='LLVIa'></small><noframes id='LLVIa'>

                  1. <tfoot id='LLVIa'></tfoot>

                    • <bdo id='LLVIa'></bdo><ul id='LLVIa'></ul>

                      <i id='LLVIa'><tr id='LLVIa'><dt id='LLVIa'><q id='LLVIa'><span id='LLVIa'><b id='LLVIa'><form id='LLVIa'><ins id='LLVIa'></ins><ul id='LLVIa'></ul><sub id='LLVIa'></sub></form><legend id='LLVIa'></legend><bdo id='LLVIa'><pre id='LLVIa'><center id='LLVIa'></center></pre></bdo></b><th id='LLVIa'></th></span></q></dt></tr></i><div id='LLVIa'><tfoot id='LLVIa'></tfoot><dl id='LLVIa'><fieldset id='LLVIa'></fieldset></dl></div>