Revision [17505]

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

 

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}}**
	- **total+taxes**:  **{{calc total+total*18.6/100.0}}**

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