Revision [17501]

This is an old revision of CalcInfo made by JfDelesse on 2007-09-22 17:07:35.

 

This action allows you to write calculations (with variables) and write out the result.

Basic usage
By typing:
1 day ={{calc 24*60*60}} seconds

you will obtain:
1 day =86400 seconds

With variables usage
By typing:
{{calc total=0}}
	- red:		  	  {{calc q=2;total+=q;q}}
	- blue:			  {{calc q=3;total+=q;q}}
	- yellow:		{{calc q=6;total+=q;q}}
	- **total**:		**{{calc total}}**

You will obtain: Because:

Code
Save this as actions/calc.php
  1. <?php
  2.  
  3. /*
  4.  *
  5.  *
  6.  */
  7.  if (!function_exists('strtonum'))
  8. {
  9.   $calcvar=array();
  10.  
  11.   function strtonum($str)
  12.     {
  13.       static $calcvar;
  14.       $pattern = '/([^;])+/';
  15.       $varpat='([a-zA-Z]+\s*\(*)';
  16.       $varpattern="/$varpat/";
  17.       $otherpat='([0-9*\-+\/=]+)';
  18.       $allpattern="/$varpat|$otherpat/";
  19.       //      print ($allpattern);
  20.       preg_match_all($pattern,$str,$blocks);
  21.       foreach ($blocks[0] as $k=>$block)
  22.     {
  23.       $affichage=true;
  24.       $blockstr="";
  25.       preg_match_all($allpattern,$block,$out,PREG_SET_ORDER);
  26.       foreach ($out as $k2=>$token)
  27.         {
  28.           if ($token[1]!="")
  29.         {
  30.           $token[1]=preg_replace('/\s*\(*/','',$token[1]);
  31.           $tokstr='$calcvar[\''.$token[1].'\']';
  32.         }
  33.           if ($token[2]!="")
  34.         {
  35.           $tokstr=$token[2];
  36.  
  37.           if (!(strpos($tokstr,"=")===false))
  38.             {
  39.               $affichage=false;
  40.             }
  41.         }
  42.           $blockstr.=$tokstr;
  43.         }
  44.       if(!empty($blockstr))
  45.         {
  46.  
  47.         $blockstr="$blockstr;";
  48.           $blockstr=eval("return ".$blockstr);
  49.           if ($affichage)
  50.         $outstr.=($outstr!=""?";":"").$blockstr ;
  51.         }
  52.  
  53.     }
  54.     return $outstr;
  55. }
  56. }
  57.  
  58. if (is_array($vars))
  59. {
  60.   print strtonum($vars['wikka_vars']);
  61.  
  62. }
  63.  
  64. ?>
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki