Jump to content

Numbers and Symbols


iversonm

Recommended Posts

Right, so a number is -?[0-9]+. Either of the operators +, -, *, / and ^ require a number on both sides.

 

preg_match('#^-?[0-9]+(?:[-+*/^](?:\(-[0-9]+\)|[0-9]+))*$', $string);

 

So basically, you can do 1*2, but you can't do 1*-2 because it needs to be 1*(-2). I didn't test it very thoroughly, but I'm pretty sure it works. It can probably be optimized as well.

Link to comment
https://forums.phpfreaks.com/topic/185140-numbers-and-symbols/#findComment-977313
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.