madorin/matex
Composer 安装命令:
composer require madorin/matex
包简介
PHP Mathematical expression parser and evaluator
README 文档
README
Matex
PHP Mathematical expression parser and evaluator
Features
- Fast evaluation
- Compact codebase
- Operators: + - * / ^ %
- Brackets, nested, unlimited levels
- Variables: predefined or estimated dynamically
- Functions: predefined or connected dynamically
- String arguments in functions, like field("name")
- String operations, currently concatenation is supported
Installation
Using Composer run
$ composer require madorin/matex
See manual for more details and options.
Usage
Basic:
$evaluator = new \Matex\Evaluator(); echo $evaluator->execute('1 + 2');
String concatenation:
$evaluator = new \Matex\Evaluator(); echo $evaluator->execute('"String" + " " + "concatenation"');
Variables:
$evaluator = new \Matex\Evaluator(); $evaluator->variables = [ 'a' => 1, 'b' => 2 ]; echo $evaluator->execute('a + b');
Dynamic variables:
public function doVariable($name, &$value) { switch ($name) { case 'b': $value = 2; break; } } $evaluator = new \Matex\Evaluator(); $evaluator->variables = [ 'a' => 1 ]; $evaluator->onVariable = [$this, 'doVariable']; echo $evaluator->execute('a + b');
Functions:
static function sum($arguments) { $result = 0; foreach ($arguments as $argument) $result += $argument; return $result; } $evaluator = new \Matex\Evaluator(); $evaluator->functions = [ 'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null] ]; echo $evaluator->execute('sum(1, 2, 3)');
Extravaganza:
/* Dynamic variable resolver Invoked when the variable is not found in the cache Returns the value by name */ public function doVariable($name, &$value) { switch ($name) { case 'zen': // Here may be a database request, or a function call $value = 999; break; case 'hit': $value = 666; break; } } /* Dynamic function resolver Invoked when the function is not found in the cache Returns an associative array array with: ref - Function reference arc - Expected argument count */ public function doFunction($name, &$value) { switch ($name) { case 'cos': // Map to a system function $value = ['ref' => 'cos', 'arc' => 1]; break; case 'minadd': // Map to a public object instance function $value = ['ref' => [$this, 'minAdd'], 'arc' => 2]; break; } } /* Custom functions, may be a - Built-in function - Global defined function - Static class function - Object instance function */ static function sum($arguments) { $result = 0; foreach ($arguments as $argument) $result += $argument; return $result; } // Just a sample custom function function minAdd($a, $b) { $r = $a < 2 ? 2 : $a; return $r + $b; } // Let's do some calculations $evaluator = new \Matex\Evaluator(); $evaluator->variables = [ 'a' => 1, 'bet' => -10.59, 'pi' => 3.141592653589 ]; $evaluator->onVariable = [$this, 'doVariable']; $evaluator->functions = [ 'sin' => ['ref' => 'sin', 'arc' => 1], 'max' => ['ref' => 'max', 'arc' => null], 'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null] ]; $evaluator->onFunction = [$this, 'doFunction']; echo $evaluator->execute('a + MinAdd(PI * sin(zen), cos(-1.7 / pi)) / bet ^ ((A + 2) * 2) + sum(5, 4, max(6, hit))');
See examples for code samples.
Author
Dorin Marcoci - dorin.marcoci@gmail.com - https://www.marcodor.com
License
Matex is distributed under MIT license.
madorin/matex 适用场景与选型建议
madorin/matex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.25M 次下载、GitHub Stars 达 116, 最近一次更新时间为 2020 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「math」 「formula」 「expression」 「calculator」 「eval」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 madorin/matex 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 madorin/matex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 madorin/matex 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LaTeX formula to PNG
Making life easier while writing complex math formulas, take a breath
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Parse excel formula to syntax tree
An MT940 bank statement parser for PHP
An open source PHP formula parser based on https://github.com/socialist/formula
统计信息
- 总下载量: 1.25M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 117
- 点击次数: 31
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-26