optimistex/math-expression
Composer 安装命令:
composer require optimistex/math-expression
包简介
Taken from http://www.phpclasses.org/browse/file/11680.html, cred to Miles Kaufmann
README 文档
README
Safely evaluate math and boolean expressions.
Install
$ composer require optimistex/math-expression
SYNOPSIS
<?php $e = new \optimistex\expression\MathExpression(); // basic evaluation: $result = $e->evaluate('2+2'); // supports: order of operation; parentheses; negation; built-in functions $result = $e->evaluate('-8(5/2)^2*(1-sqrt(4))-8'); // support of booleans $result = $e->evaluate('10 < 20 || 20 > 30 && 10 == 10'); // support for strings and match (regexes can be like in php or like in javascript) $result = $e->evaluate('"Foo,Bar" =~ /^([fo]+),(bar)$/i'); // previous call will create $0 for whole match match and $1,$2 for groups $result = $e->evaluate('$2'); // create your own variables $e->evaluate('a = e^(ln(pi))'); // or functions $e->evaluate('f(x,y) = x^2 + y^2 - 2x*y + 1'); // and then use them $result = $e->evaluate('3*f(42,a)'); // create external functions $e->functions['foo'] = function() { return "foo"; }; // and use it $result = $e->evaluate('foo()'); ?>
DESCRIPTION
This class can be used to safely evaluate mathematical expressions.
The class can take an expression in a text string and evaluate it by replacing values of variables and calculating the results of mathematical functions and operations.
It supports implicit multiplication, multivariable functions and nested functions.
It can be used to evaluate expressions from untrusted sources. It provides robust error checking and only evaluates a limited set of functions.
It could be used to generate graphs from expressions of formulae.
Use the Expression class when you want to evaluate mathematical or boolean
expressions from untrusted sources. You can define your own variables and
functions, which are stored in the object. Try it, it's fun!
Based on http://www.phpclasses.org/browse/file/11680.html, cred to Miles Kaufmann
METHODS
$e->evalute($expr)
Evaluates the expression and returns the result. If an error occurs,
prints a warning and returns false. If $expr is a function assignment,
returns true on success.
$e->e($expr)
A synonym for $e->evaluate().
$e->vars()
Returns an associative array of all user-defined variables and values.
$e->funcs()
Returns an array of all user-defined functions.
PARAMETERS
$e->suppress_errors
Set to true to turn off warnings when evaluating expressions
$e->last_error
If the last evaluation failed, contains a string describing the error.
(Useful when suppress_errors is on).
AUTHORS INFORMATION
Copyright 2005, Miles Kaufmann.
Copyright 2016, Jakub Jankiewicz
Copyright 2016, Konstantin Polyntsov
Version 2.0
Contribute
For running tests run
docker compose up -d docker compose exec php8.3 composer install docker compose exec php8.3 composer test
LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1 Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
optimistex/math-expression 适用场景与选型建议
optimistex/math-expression 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.94k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2017 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「math」 「expression」 「math expression」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 optimistex/math-expression 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 optimistex/math-expression 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 optimistex/math-expression 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LaTeX formula to PNG
Business rules engine tools.
Partition problem for balanced arrays splitting made easy.
Simple, chainable calculation library.
Easy to use arithmetic operations for decimals in PHP (using BCMath)
Core logic for elegant graphic visualization. Render to SVG, PNG, JPG and Base64.
统计信息
- 总下载量: 6.94k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-02