This action allows you to put calculations (with variables or not) 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:

Features


Restrictions

Improvements

Code
Save this as actions/calc.php
  1. <?php
  2.  
  3. /*
  4.  *Author:Jean-françois Delesse ([email protected])
  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.       preg_match_all($pattern,$str,$blocks);
  19.       foreach ($blocks[0] as $k=>$block)
  20.     {
  21.       $affichage=true;
  22.       $blockstr="";
  23.       preg_match_all($allpattern,$block,$out,PREG_SET_ORDER);
  24.       foreach ($out as $k2=>$token)
  25.         {
  26.           if ($token[1]!="")
  27.         {
  28.           $token[1]=preg_replace('/\s*\(*/','',$token[1]);
  29.           $tokstr='$calcvar[\''.$token[1].'\']';
  30.         }
  31.           if ($token[2]!="")
  32.         {
  33.           $tokstr=$token[2];
  34.  
  35.           if (!(strpos($tokstr,"=")===false))
  36.             {
  37.               $affichage=false;
  38.             }
  39.         }
  40.           $blockstr.=$tokstr;
  41.         }
  42.       if(!empty($blockstr))
  43.         {
  44.         $blockstr="$blockstr;";
  45.           $blockstr=eval("return ".$blockstr);
  46.           if ($affichage)
  47.         $outstr.=($outstr!=""?";":"").$blockstr ;
  48.         }
  49.     }
  50.     return $outstr;
  51.       }
  52. }
  53.  
  54. if (is_array($vars))
  55. {
  56.   print strtonum($vars['wikka_vars']);
  57. }
  58. ?>
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki