{"id":17309587,"url":"https://github.com/jcubic/expression.php","last_synced_at":"2025-04-14T13:52:54.547Z","repository":{"id":226554905,"uuid":"741883985","full_name":"jcubic/expression.php","owner":"jcubic","description":"Safely evaluate math, string, and boolean expressions","archived":false,"fork":false,"pushed_at":"2024-04-13T14:24:32.000Z","size":233,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-21T11:51:05.418Z","etag":null,"topics":["evaluator","expression","interpreter","math","parser"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jcubic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-01-11T10:06:13.000Z","updated_at":"2024-01-11T10:07:49.000Z","dependencies_parsed_at":"2024-03-08T08:26:39.980Z","dependency_job_id":"c4691a9e-ba04-4684-9f1e-b93f606dfe81","html_url":"https://github.com/jcubic/expression.php","commit_stats":null,"previous_names":["jcubic/expression.php"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcubic%2Fexpression.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcubic%2Fexpression.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcubic%2Fexpression.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcubic%2Fexpression.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcubic","download_url":"https://codeload.github.com/jcubic/expression.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248891387,"owners_count":21178633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["evaluator","expression","interpreter","math","parser"],"created_at":"2024-10-15T12:30:12.141Z","updated_at":"2025-04-14T13:52:54.515Z","avatar_url":"https://github.com/jcubic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/jcubic/expression.php/blob/master/.github/logo.svg?raw=true\"\n       alt=\"Expression.php - safely evaluate math, string, and boolean expressions\" /\u003e\n\u003c/h1\u003e\n\n[Safely evaluate math, string, and boolean expressions](https://github.com/jcubic/expression.php/)\n\n[![Latest Stable Version](https://poser.pugx.org/jcubic/expression/v/stable.svg)](https://packagist.org/packages/jcubic/expression)\n[![CI](https://github.com/jcubic/expression.php/actions/workflows/test.yaml/badge.svg)](https://github.com/jcubic/expression.php/actions/workflows/test.yaml)\n[![Total Downloads](https://poser.pugx.org/jcubic/expression/downloads.svg)](https://packagist.org/packages/jcubic/expression)\n[![License](https://poser.pugx.org/jcubic/expression/license.svg)](https://packagist.org/packages/jcubic/expression)\n\n## DESCRIPTION\n\nUse the Expression class when you want to evaluate mathematical or boolean expressions from\nuntrusted sources. You can define your own variables and functions, which are stored in the object.\n\n## FEATURES\n* Integers and floats\n* Math Expressions: `*` `/` `-` `+` `%`, `^`, and `**`\n* Boolean Expression: `\u0026\u0026`, `||`, and `!`\n* Comparisons: `\u003e` `\u003c` `==` `!=` `\u003c=` `\u003e=`\n* Strict comparison `===` and `!==`\n* `pi` and `e` constants\n* Regular Expressions and match operator `=~`\n* String literals\n* JSON objects and Arrays\n* Square brackets operation on objects and arrays\n* Bit shift operators `\u003e\u003e` `\u003c\u003c`\n* Equal operator works on arrays and objects\n* Functions and variables\n\n\n## INSTALLATION\n\n```bash\ncomposer require jcubic/expression\n```\n\n## USAGE\n```php\n\u003c?\n\nrequire_once(__DIR__ . \"/vendor/autoload.php\");\nuse jcubic\\Expression;\n\n$e = new Expression();\n// basic evaluation:\n$result = $e-\u003eevaluate('2+2');\n// supports: order of operation; parentheses; negation; built-in functions\n$result = $e-\u003eevaluate('-8(5/2)^2*(1-sqrt(4))-8');\n// support of booleans\n$result = $e-\u003eevaluate('10 \u003c 20 || 20 \u003e 30 \u0026\u0026 10 == 10');\n// support for strings and match (regexes can be like in php or like in javascript)\n$result = $e-\u003eevaluate('\"Foo,Bar\" =~ /^([fo]+),(bar)$/i');\n// previous call will create $0 for whole match match and $1,$2 for groups\n$result = $e-\u003eevaluate('$2');\n// create your own variables\n$e-\u003eevaluate('a = e^(ln(pi))');\n// or functions\n$e-\u003eevaluate('f(x,y) = x^2 + y^2 - 2x*y + 1');\n// and then use them\n$result = $e-\u003eevaluate('3*f(42,a)');\n// create external functions\n$e-\u003efunctions['foo'] = function() {\n  return \"foo\";\n};\n// and use them\n$result = $e-\u003eevaluate('foo()');\n```\n\n## METHODS\n\n* `$e-\u003eevalute($expr)`\n\nEvaluates the expression and returns the result.  If an error occurs,\nprints a warning and returns false.  If $expr is a function assignment,\nreturns true on success.\n\n* `$e-\u003ee($expr)`\n\nA synonym for $e-\u003eevaluate().\n\n* `$e-\u003evars()`\n\nReturns an associative array of all user-defined variables and values.\n\n* `$e-\u003efuncs()`\n\nReturns an array of all user-defined functions.\n\n## PARAMETERS\n* `$e-\u003esuppress_errors`\n\nSet to true to turn off warnings when evaluating expressions\n\n* `$e-\u003elast_error`\n\nIf the last evaluation failed, contains a string describing the error.\n(Useful when suppress_errors is on).\n\n* `$e-\u003efunctions`\n\nAssoc array that contains functions defined externally.\n\n* `$e-\u003evariables`\n\nAssoc array that contains variables defined by user and externally.\n\n## HISTORY\nThis project started as a fork. Original code was created by\n[Miles Kaufmann ](http://www.phpclasses.org/browse/file/11680.html) and published\non PHPClasses.org. I've added a lot of features and bug fixes to original code,\nbut then decided that the code is really hard to modify to add new features and\nfix bugs. So I decide to rewrite everything from scratch using\n[PEG](https://en.wikipedia.org/wiki/Parsing_expression_grammar) parser generator.\n\nThe original code is still available as version 1.0 on\n[packagist](https://packagist.org/packages/jcubic/expression#1.0.2) and\nthe source code you can find in\n[legacy branch](https://github.com/jcubic/expression.php/tree/legacy).\n\n## ACKNOWLEDGMENTS\n* Logo uses:\n  * icon from [Clarity icon set](https://github.com/jcubic/Clarity)\n  * [Prosto one font](https://www.1001fonts.com/prosto-one-font.html) by [Pavel Emelyanov](https://www.behance.net/Levap)\n* The public API is taken from the original code by Miles Kaufmann\n\n## LICENSE\nCopyright (c) 2024 [Jakub T. Jankiewicz](https://jakub.jankiewicz.org)\u003cbr/\u003e\nReleased under [MIT](http://opensource.org/licenses/MIT) license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcubic%2Fexpression.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcubic%2Fexpression.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcubic%2Fexpression.php/lists"}