fork download
  1. <?php
  2. $string = 'A>B
  3. A>B AND B=C
  4. A>C AND C<B
  5. A<B AND B<C AND A>C
  6. A<B AND B<C AND A>C AND A<D';
  7.  
  8. $array = preg_split("#\r\n?|\n#", $string);
  9. foreach($array as $cond){
  10. echo htmlspecialchars($cond) .' : '.(compare($cond) == true ? 'true':'false').'<br>';
  11. }
  12.  
  13. function compare($condition){
  14. $chunks = explode(' ', $condition);
  15. foreach($chunks as $chunk){
  16. if($chunk !== 'AND'){
  17. preg_match('#([a-z])([<>=])([a-z])#i', $chunk, $m);
  18. if(!isset(${$m[1]})){${$m[1]}= 10000;}
  19. if(!isset(${$m[3]})){
  20. switch($m[2]){
  21. case '<':
  22. ${$m[3]} = ${$m[1]} * 2;
  23. break;
  24. case '>':
  25. ${$m[3]} = ${$m[1]} / 2;
  26. break;
  27. case '=':
  28. ${$m[3]} = ${$m[1]};
  29. break;
  30. }
  31. }
  32. }
  33. }
  34. $variabled = preg_replace('#\b[a-z]\b#i', '\$$0', $condition);
  35. eval('$eval = ('.$variabled.') ? true:false;');
  36. return $eval;
  37. }
  38. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
A&gt;B : true<br>A&gt;B AND B=C : true<br>A&gt;C AND C&lt;B : true<br>A&lt;B AND B&lt;C AND A&gt;C : false<br>A&lt;B AND B&lt;C AND A&gt;C AND A&lt;D : false<br>