- preg_match: documentation ( source)
- file_put_contents: documentation ( source)
<?php
$_GET['msg']="what is 5 + 5 ?";
$msg=$_GET['msg']??"no msg!";
file_put_contents('msg.log.txt',$msg."\r\n",FILE_APPEND|LOCK_EX);
$matches=NULL;
$isMatch=preg_match("/(\d+)\s*(\+|\-|\*|\/|\:)\s*(\d+)/",$msg,$matches);
if($isMatch){
$l=(int)$matches[1];
$o=$matches[2];
$r=(int)$matches[3];
switch($o){
case '+':
$res=$l+$r;
break;
case '-':
$res=$l-$r;
break;
case '*':
$res=$l*$r;
break;
case '\\':
case ':':
$res=$l/$r;
break;
default:
throw new \LogicException("invalid operator passed regex??!");
}
echo "!check {$res}";
}