Hey all,
I’ve been working on this little script to replace values in a stored equation (database) with actual numbers and then running the equation. But all I keep getting is just the script echoing out the string instead of evaluating it.
I know what I’m going to post is kinda long and the script isn’t complete yet. It’s just a proof of concept before I move forward with a larger portion of the project. any help would be much appreciated:)
$eq = 10*5 / p1.str + p1.dex / p1.chr;
replaceHoldersWithVals(createStatArr(getHolders($eq)),$eq);
function getHolders($str)
{
$statArr = array();
$pos = 0;
$i = 0;
$len = strlen($str);
while($i != strlen($str))
{
if(stripos($str,"p1",$pos))
{
$pos = stripos($str,"p1",$pos);
array_push($statArr,substr($str,$pos+3,3));
$pos += 6;
}else{
$i = $len;
}
}
return $statArr;
}
function createStatArr($statArr)
{
for($i = 0; $i < count($statArr);$i++)
{
switch($statArr[$i])
{
case "str":
{
$statArr[$i] = array("str"=>10);
break;
}
case "dex":
{
$statArr[$i] = array("dex"=>7);
break;
}
case "chr":
{
$statArr[$i] = array("chr"=>2);
break;
}
}
}
return $statArr;
}
function replaceHoldersWithVals($statArr, $str)
{
$pos = 0;
$len = strlen($str);
for($i = 0; $i < count($statArr);$i++)
{
$str = str_ireplace("p1.".key($statArr[$i]),$statArr[$i][key($statArr[$i])],$str);
}
eval("\$e = \"$str\";");
echo $e;
}