php - Excluding all characters NOT in list AND NOT in a list of phrases -


i'm attempting make function in php evaluate mathematical expression -- including functions such sin, cos, etc. approach delete characters in phrase not numbers, mathematical operators, or mathematical functions , use string in eval(). problem don't know enough regular expressions negate both characters , phrases in same expression.

so far, i've got:

$input = preg_replace("/[^0-9+\-.*\/()sincota]/", "", $input); 

obviously, characters sin, cos, , tan can used in order in input expression (rather only allowing phrases sin, cos, , tan). if further expand function include more characters , functions, presents bigger security risk malicious user able execute php command through clever interaction app.

can tell me how fix regex , eliminate problem?

i'm attempting make function in php evaluate mathematical expression -- including functions such sin, cos, etc

might suggest taking advantage of years of work has been put phpexcel, includes formula parser already.

it includes cos, sin , hundreds of others.

otherwise, rather negating, can positive matches:

$matches = array(); preg_match_all("#([0-9,/\*()+\s\.-]|sin|cos)+#", 'sin(12) + cos(13.5/2) evddal * (4-1)', $matches); echo implode('', $matches[0]);  /* output: sin(12) + cos(13.5/2) * (4-1)  */ 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -