This should be pretty secure:
function do_maths($expression) {
eval('$o = ' . preg_replace('/[^0-9\+\-\*\/\(\)\.]/', '', $expression) . ';');
return $o;
}
echo do_maths('1+1');
More Related Contents:
- regular expression [closed]
- How to get “v.3.1.2” in string by regex php [duplicate]
- Regex to validate range of numbers from 50 to 1000 with step of 50 [closed]
- Replace preg_replace() e modifier with preg_replace_callback
- How do I find all YouTube video ids in a string using a regex?
- Is there a PHP function that can escape regex patterns before they are applied?
- Remove new lines from string and replace with one empty space
- php Replacing multiple spaces with a single space [duplicate]
- How to mimic Stack Overflow Auto-Link Behavior
- How to make dot match newline characters using regular expressions
- Remove control characters from PHP string
- How do I make this preg_match case insensitive?
- Matching a space in regex
- Regex for names
- Strip all HTML tags, except allowed
- Parse a CSS file with PHP
- Extract URL from string
- How to split a long string without breaking words?
- How to capitalize first letter of first word in a sentence?
- How do I extract query parameters from a URL string in PHP?
- Regex to detect invalid UTF-8 string
- Change foreign characters to their roman equivalent
- Remove domain extension
- Close open HTML tags in a string
- Regex & PHP – isolate src attribute from img tag
- PHP regex for validating a URL
- Regex matching table rows in HTML [duplicate]
- How to remove all numbers from string?
- Regular expression preg_quote symbols are not detected
- Detecting a url using preg_match? without http:// in the string