Account with PHP in the order it is in the string

Asked

Viewed 197 times

4

I need an order of reckoning to be in string be done in the order it is in. For example:

$string = '(10*10)+(30-5)/2';

I need PHP to interpret this not as a string, but rather as a calculation and do according to what has to be done. How can I do this?

3 answers

8

I have decided to give a supplementary answer on the eval() which is a valid solution. This can be seen in Eval is either good or bad?.

To tell the truth any information about the eval() that I don’t have that caveat for me would be wrong.

Use the eval() is extremely dangerous. You can even use it without taking big risks, but almost no one knows how to do it, so it’s best not to try before making sure you understand all the risks and know how to solve them. It’s so complicated to get it right that it’s often better to use more complex shape than it.

The question does not make clear where this information comes from. If it comes from a customer, then forget the eval(), the work to ensure security in it is so great that making the simple formula compiler for what it needs is simpler. Maybe we can even do it with RegEx, that I do not like, but it is a solution. The solution goes through a .

If the information doesn’t come externally, then it’s likely to be safe, though, why would you use a eval() in something that does not come externally? There may be a reason, but it is unlikely to be the right mechanism. I’ve seen a lot of people using this kind of resource, for lazy of typing codes. That’s a very wrong reason.

  • This does not provide an answer to the question. To criticize or request clarification from an author, leave a comment below its publication. - From Review

  • 1

    I think it provides an answer and more important that the answer accepts, after all says that the eval() can be done, but should not be used and gives the real solution, just does not give a parser complete because it was not asked and would be very broad. I know the AP will go the easy and unsafe way, but it serves for people who care about security to understand that this is not a suitable solution.

  • 2

    I do not disagree with what I said, it is important, I only disagree with this being an answer, because it speaks how it can be done but it does not present an example to determine how a complete response, I think it would fit more as a recommendation comment.

  • 1

    But it’s still an answer, you just disagree with what was put. Actually today in moderation I see the signs and realize that many people do not know what is an answer or not, I have to refuse a lot. Note the text that is placed when you choose this option and tell me if it makes any sense here. The example could only be given by giving a code that would not even fit in a response. At least in the form that I would do. But there is a new information given, it is up to the author to ask more specific questions if he is interested, which we have seen that there is not, he has already chosen the risky solution

6


You can do it:

$string = '(10*10)+(30-5)/2';
eval( '$result = (' . $string. ');' );
echo $result;

Obs: As you must know the division comes before the sum, then the result will be: 112.5

If you want the division to last. Add one more parenthesis to your equation. See: ((10*10)+(30-5))/2

3

As a complement to the answer I have already given, I have decided to post a solution on the subject.

There is a Symfony component called Expression Language, that can facilitate your service. With it you can use simple expressions, through strings, which will be interpreted by a parser and get the result in php.

Behold:

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

$language = new ExpressionLanguage();

var_dump($language->evaluate('1 + 2')); // displays 3

var_dump($language->compile('1 + 2')); // displays (1 + 2)

I’m not sure, but it seems to me that the syntax interpreted by this library is very similar to Twig.

Nothing against the response of eval, but as said by @Maniero, it is good to take care and know what you are doing, not to put your application at risk.

  • And is there a reason he’s safer, or does he have the same problem eval()?

  • @bigown the expressions he uses, as far as I’ve seen, does not do things like: connection to bank, change files. Seems to be more of a parser for template.

  • So you don’t understand where the security problem is and you shouldn’t use that sort of thing :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.