Vivid Lust Posted December 27, 2007 Share Posted December 27, 2007 Hey, i was wondering if you could work out a sum in a txt file. Say in the text file the value was: (8+2)*4 How could i use php to read this and work it out?? My code so far is <?php $sumfile = "sum.txt"; $file = fopen( $sumfile, "w+"); $sum = fread($file, $filesize); $answer = fwrite( $file, "<font color=red>.$answer</a>"); fclose( $file ); header('Location: calc2.php'); ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/83376-solved-reading-a-sum-from-a-txt-file-and-working-it-out/ Share on other sites More sharing options...
suttercain Posted December 27, 2007 Share Posted December 27, 2007 You would have to write a function and create all the if statements yourself. Quote Link to comment https://forums.phpfreaks.com/topic/83376-solved-reading-a-sum-from-a-txt-file-and-working-it-out/#findComment-424180 Share on other sites More sharing options...
Vivid Lust Posted December 27, 2007 Author Share Posted December 27, 2007 Could you give me an example??? Im really new to php Quote Link to comment https://forums.phpfreaks.com/topic/83376-solved-reading-a-sum-from-a-txt-file-and-working-it-out/#findComment-424191 Share on other sites More sharing options...
suttercain Posted December 27, 2007 Share Posted December 27, 2007 Sure, in the file you have, the only text is the equation? There is nothing else right? Quote Link to comment https://forums.phpfreaks.com/topic/83376-solved-reading-a-sum-from-a-txt-file-and-working-it-out/#findComment-424193 Share on other sites More sharing options...
blackcell Posted December 27, 2007 Share Posted December 27, 2007 Since there are no variables, I think it would be some pretty intensive testing with if statements. You would have to baby step each character using substr and incrementing 1 within a loop. Throughout that you will have to test for the possibilities of whether or not it is an integer(in which you will be working with), a nesting character ([ ] ( ) { }) or a mathematical sign( + - / * %). That is probably the easy part. To accurately calculate the values, you will have to have a quite complex set of logical determinations to properly put the characters into variables and execute the calculation in proper sequence. Unless someone has an easier way, I would probably try to see if you can use an interface to input the numbers or something. You might try posting on the Math Based Programming Sub Forum: http://www.phpfreaks.com/forums/index.php/board,68.0.html Quote Link to comment https://forums.phpfreaks.com/topic/83376-solved-reading-a-sum-from-a-txt-file-and-working-it-out/#findComment-424194 Share on other sites More sharing options...
Vivid Lust Posted December 27, 2007 Author Share Posted December 27, 2007 Well im making a calcluator so there can be any of the following: 1,2,3,4,5,6,7,8,9,0,/,*,+,-,(,) Quote Link to comment https://forums.phpfreaks.com/topic/83376-solved-reading-a-sum-from-a-txt-file-and-working-it-out/#findComment-424203 Share on other sites More sharing options...
ohdang888 Posted December 27, 2007 Share Posted December 27, 2007 you never specify what $filesize is Quote Link to comment https://forums.phpfreaks.com/topic/83376-solved-reading-a-sum-from-a-txt-file-and-working-it-out/#findComment-424212 Share on other sites More sharing options...
suttercain Posted December 27, 2007 Share Posted December 27, 2007 <?php $file = file("file.txt"); //file only contains the equation (8+2)*4 and nothing else! $zipped = implode('', $file); function strtonum($str) { $str = preg_replace('`([^+\-*=/\(\)\d\^<>&|\.]*)`','',$str); if(empty($str))$str = '0'; else eval("\$str = $str;"); return $str; } echo strtonum("$zipped"); // echos 40... the correct sum ?> Quote Link to comment https://forums.phpfreaks.com/topic/83376-solved-reading-a-sum-from-a-txt-file-and-working-it-out/#findComment-424215 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.