marcus Posted January 30, 2009 Share Posted January 30, 2009 <?php $equation = "x^3 + 2x^2 + 5x - 3"; $solutions = 10; $new = str_replace(" ",null,$equation); $find = "/([a-z])\^([\d]+)/"; $repl = '\1<sup>\2</sup>'; $do = preg_replace($find,$repl,$equation); echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" style=\"border:1px solid #000\">\n"; echo "<tr><td colspan=\"2\" align=\"center\"><b>".$do."</b></td></tr>\n"; echo "<tr><td align=\"center\">X</td><td align=\"center\">Y</td></tr>\n"; for($i=(0-$solutions);$i<=$solutions;$i++){ $xf = "/([a-z])\^([\d]+)/"; $xr = pow('\$1','\$2'); $xf2 = "/([\d]+)([x])/"; $xr2 = '\1*'.$i; $xf3 = "/([\d]+)\^([\d]+)/"; $xr3 = 'pow(\1,\2)'; $orange = preg_replace($xf2,$xr2,$new); #$apple = preg_replace($xf,$xr,$orange); $apple = preg_replace ("/([a-z])\^([\d]+)/","pow($1,$2)",$orange); $banana = preg_replace($xf3,$xr3,$apple); $final = str_replace("x",$i,$banana); echo "<tr><td align=\"center\" style=\"border:1px solid #000\">".$i."</td><td align=\"center\" style=\"border:1px solid #000\">".$final."</td></tr>\n"; } echo "</table>\n"; ?> From the code you can see I'm making the equation's variable replaced with $i, how can I successfully create the exact output of each f(x) ? Link to comment https://forums.phpfreaks.com/topic/143071-convert-formula-into-generated-outputs/ Share on other sites More sharing options...
amites Posted January 30, 2009 Share Posted January 30, 2009 looks like you could use an array $x = array( 'f' => "/([a-z])\^([\d]+)/", 'r' => pow('\$1','\$2'), 'f2' => "/([\d]+)([x])/", etc... foreach ($x as $val => $var) { echo $val . ' = ' . $var . "\n<br>\n"; } Link to comment https://forums.phpfreaks.com/topic/143071-convert-formula-into-generated-outputs/#findComment-750383 Share on other sites More sharing options...
marcus Posted January 30, 2009 Author Share Posted January 30, 2009 Don't see how that's going to help me. Link to comment https://forums.phpfreaks.com/topic/143071-convert-formula-into-generated-outputs/#findComment-750540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.