• Stars
    star
    1
  • Language
    PHP
  • License
    MIT License
  • Created 4 months ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Safely evaluate math, string, and boolean expressions

Expression.php - safely evaluate math, string, and boolean expressions

Safely evaluate math, string, and boolean expressions

Latest Stable Version CI Total Downloads License

DESCRIPTION

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.

FEATURES

  • Integers and floats
  • Math Expressions: * / - + %, ^, and **
  • Boolean Expression: &&, ||, and !
  • Comparisons: > < == != <= >=
  • Strict comparison === and !==
  • pi and e constants
  • Regular Expressions and match operator =~
  • String literals
  • JSON objects and Arrays
  • Square brackets operation on objects and arrays
  • Bit shift operators >> <<
  • Equal operator works on arrays and objects
  • Functions and variables

INSTALLATION

composer require jcubic/expression

USAGE

<?

require_once(__DIR__ . "/vendor/autoload.php");
use jcubic\Expression;

$e = new Expression();
// 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 them
$result = $e->evaluate('foo()');

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).

  • $e->functions

Assoc array that contains functions defined externally.

  • $e->variables

Assoc array that contains variables defined by user and externally.

HISTORY

This project started as a fork. Original code was created by Miles Kaufmann and published on PHPClasses.org. I've added a lot of features and bug fixes to original code, but then decided that the code is really hard to modify to add new features and fix bugs. So I decide to rewrite everything from scratch using PEG parser generator.

The original code is still available as version 1.0 on packagist and the source code you can find in legacy branch.

ACKNOWLEDGMENTS

LICENSE

Copyright (c) 2024 Jakub T. Jankiewicz
Released under MIT license

More Repositories

1

jquery.terminal

jQuery Terminal Emulator - JavaScript library for creating web-based terminals with custom commands
JavaScript
2,940
star
2

sysend.js

Web application synchronization between different tabs
JavaScript
1,025
star
3

wayne

Service Worker Routing library for in browser HTTP requests
JavaScript
531
star
4

lips

Scheme based powerful lisp interpreter written in JavaScript
JavaScript
323
star
5

chat-gpt

ChatGPT conversation saving bookmark
JavaScript
275
star
6

tagger

Zero Dependency, Vanilla JavaScript Tag Editor
JavaScript
258
star
7

jquery.splitter

jQuery Splitter is plugin that split your content with movable splitter between them
JavaScript
240
star
8

gaiman

Gaiman: Text based game engine and programming language
JavaScript
130
star
9

leash

Browser Shell
JavaScript
120
star
10

jquery.rotate

Simple plugin that add rotate css property and animation
JavaScript
94
star
11

git

GIT Web Terminal
JavaScript
82
star
12

Clarity

Customizable Monoshape Vector Icon Theme for GTK+
SVG
39
star
13

jquery.filebrowser

File browser jQuery plugin
JavaScript
30
star
14

jquery.terminal-www

jQuery Terminal Website
HTML
26
star
15

favloader

Vanilla JavaScript library for loading animation in favicon
JavaScript
24
star
16

lily

Simple JavaScript options parser inspired by yargs
JavaScript
21
star
17

jsh.php

Terminal like php shell (PHP web terminal emulator)
PHP
20
star
18

jsvi

fork of JSVI - VI in JavaScript
JavaScript
18
star
19

cmatrix

Render Matrix effect animation on Canvas in JavaScript
JavaScript
17
star
20

Monadic

JavaScript micro library - POC
JavaScript
16
star
21

fake-linux-terminal

Fake GNU/Linux, Unix, MacOS terminal based system
HTML
13
star
22

json-rpc

JSON-RPC implementaion in php and JavaScript
PHP
13
star
23

electron-terminal

Base for jQuery Terminal based Electron apps
JavaScript
12
star
24

route.js

Simple routing library that can be use on the server or in the browser
JavaScript
11
star
25

jquery.micro

Pico/Nano like editor for jquery.
JavaScript
11
star
26

coverage.el

Emacs minor mode for displaying code coverage
Emacs Lisp
10
star
27

chat

Simple Chat with Server Side Events, PHP and SQLite
PHP
8
star
28

ascii-canvas

String based text rendering for Node and Browser
JavaScript
7
star
29

prism-cli

Syntax highlighting for command line
JavaScript
7
star
30

try-python

Try Python website
Python
7
star
31

commodore64

Commodore 64 jQuery Terminal Demo
HTML
7
star
32

jquery.resize

Custom resize jQuery event for element
JavaScript
6
star
33

notes

Simple text based notes taking app
JavaScript
6
star
34

jcubic.pl

Głównie JavaScript - blog głównie o Front-Endzie
PHP
5
star
35

awesome-ascii

List of Awesome ASCII libraries
5
star
36

ansidec

Unix formatting transformer in JavaScript
JavaScript
5
star
37

firepad

Simple firepad based source code editor
HTML
5
star
38

webrtc-share

Application for sharing files using WebRTC
PHP
5
star
39

quizerach

Simple Quiz Maker Open Source App (WIP)
TypeScript
4
star
40

calendar

ASCII calendar that can be used in Node.js or Browser
JavaScript
4
star
41

angular.piechart

Angular 1.5 component for svg based piecharts
JavaScript
4
star
42

velvet

Vanilla JavaScript Universal CSS in JS library
TypeScript
4
star
43

chess

Terminal based chess game
HTML
3
star
44

open-source-library

Template for JavaScript Open Source library
Makefile
3
star
45

snapp

Simple Text based notes taking app
PHP
3
star
46

ToME

ToME - Tales of Middle Earth
C
3
star
47

leash-cordova

Cordova leash shell
JavaScript
3
star
48

filter-paste-js

intercept content pasting into iframe with design mode
JavaScript
3
star
49

fs-browser

In Browser File System App
JavaScript
3
star
50

jquery-position-event

jQuery cursor position change event
JavaScript
2
star
51

jankiewicz

Personal Blog of Jakub T. Jankiewicz
Liquid
2
star
52

koduj-curriculum

Curriculum for teaching programming using JavaScript and p5.js library
2
star
53

dotfiles

Linux dotfiles
Shell
2
star
54

json-rpc-list

List of JSON-RPC implementations
2
star
55

compickr

Flickr composition checker
JavaScript
2
star
56

kopalinski.sqlite

Słownik Wyrazów Obcych Kopalińskiego jako baza sqlite
Python
2
star
57

cataloger

Shopping cart like catalog library
JavaScript
2
star
58

aimed

Simple JavaScript Markdown Editor
JavaScript
2
star
59

koduj

P5.js playground
PHP
2
star
60

opensourcelogo

Logo for you Open Source project
2
star
61

yapp

Yet Another PHP Proxy
JavaScript
2
star
62

jcubic

1
star
63

price.py

Scraping prices from Ceneo.pl and save in SQLite database
Python
1
star
64

php-terminal-jwt

Demo of JWT with jQuery Terminal
PHP
1
star
65

flash-3d

School project 3D Flash animation in C++
C
1
star
66

jquery.draglessClick

Better jQuery click event that's not invoked when you drag or select text
JavaScript
1
star
67

FizzBuzz

JavaScript version of function only FizzBuzz
JavaScript
1
star
68

bassel.jcubic.pl

Bassel badge page
PHP
1
star
69

roman

ReactJS application with unit tests
TypeScript
1
star
70

swift.manager

Server file browser and terminal with apps support
JavaScript
1
star
71

bash

Bash Interpreter written in JavaScript
1
star
72

jsvi-app

jsvi app for leash shell
JavaScript
1
star
73

refClass

R
1
star
74

Downloader

Command Line tool for download file from file hosting sites.
Ruby
1
star
75

cv

My resume
Makefile
1
star
76

Similar-Stuff

Get Similar stuff from tastekid.com
Python
1
star
77

static

static files used with cdn.jsdelivr.net
JavaScript
1
star
78

REPL

REPL Bookmarklets for different languages
1
star
79

quatro

Simple Q&A Open Source application in PHP
PHP
1
star
80

interactive-scheme-tutorial

Interactive Scheme Tutorial
HTML
1
star
81

gps.py

Simple script to add GPS EXIF data to images
Python
1
star
82

uncp

UNsplash Cache Proxy
PHP
1
star