将字符串转换为数学方程式 [英] Convert string into Math equation

查看:99
本文介绍了将字符串转换为数学方程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想将包含一个方程式的字符串转换为实际的PHP Math方程式,使用PHP5 +可以吗?如果可以,怎么办?

I'm simply wanting to convert a string which contains an equation into an actual PHP Math Equation, is this possible using PHP5+ ? If so, how?

以下是该字符串的一些示例:

Here's some examples of which the string may be:

$string = "37-0";
$string = "315+10";
$string = "25+50";
$string = "88-13";

如果可能的话,我只是想将其解析为正确的数学答案

I simply want to parse these into the correct Mathematical answer if that's possible

推荐答案

尝试一下:

$str = '37 - 0';
eval( '$result = (' . $str. ');' );
echo $result;

检查评估函数

将给定的代码评估为PHP.

Evaluates the given code as PHP.

附带说明:

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.

这篇关于将字符串转换为数学方程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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