1<?php
2
3/**
4* pass through  the input
5* replace the string in: { ?/@/#/! string }  with the value of one or more potids value
6*/
7
8//$potformula = preg_replace('/<|>/', '_', $match); //replace tags like <div>  to _div_
9$potformula = htmlspecialchars($match);
10
11$pattern = '/\s*\{{1}\s*([\?@!#&)]?)\s*(.*?)\s*\}{1}\s*/';
12$complete = preg_replace_callback($pattern, function ($stew) {
13	$potid = $this->cargo['pot'];
14	$all = $stew[0];
15	$spice = $stew[1];
16	$searchpotid = $stew[2];
17	$completeformula = '';
18
19	switch ($spice) {
20		case '@': // get the result of exactly one potid
21			$thepotid = $this->cargo[$searchpotid]['output']['result'];
22			if (isset($thepotid)) {
23				$completeformula .= $this->cargo[$searchpotid]['output']['formula'];
24			} else {
25				$completeformula .= " 'not set: {@$searchpotid}' ";
26				$this->cargo[$potid]['output']['error'] = " error:";
27			}
28			break;
29		case '#': //sum values with matching string in input "{# string}"
30			$forkey = '';
31			foreach ($this->cargo as $key => $value) {
32				preg_match("/.*($searchpotid).*/x", $key, $match); //.*(gut).*
33				if ($key == $match[0]) {
34					if (isset($value['output']['result'])) {
35						$forkey .= " ".$value['output']['result'];
36					}
37				}
38			}
39			$completeformula .= ltrim($forkey, ' ') ;
40			break;
41		case '&': // sum results matching the pot id of the asking potid
42			preg_match('/^(.\*?)(\d\*?)$/', $this->cargo['pot'], $potidarr); //fetch potid of this potid
43			$searchval = $potidarr[1]; // only first part, no mumber
44			$sumarr = array();
45			foreach ($this->cargo as $key => $value) {
46				preg_match("/.*($searchval).*/", $key, $match);
47
48				if ($key == $match[0]) {
49					if (isset($value['output']['result'])) {
50						$forkey .= " ".$value['output']['result'];
51					}
52				}
53			}
54			$completeformula .=  ltrim($forkey, ' ');
55			break;
56		default:
57			$completeformula .= $all;
58			break;
59	}
60
61	return $completeformula;
62
63}, $potformula);
64
65// print("<pre>  completeformula:  ".print_r($complete, true)."</pre>");
66
67$this->cargo[$potid]['output']['result'] = $complete; // set the var to use it global
68$this->cargo[$potid]['output']['formula'] = $complete;
69$this->cargo[$potid]['output']['type'] = 'text';
70if ($error != '') {
71	$this->cargo[$potid]['output']['error'] = 'error  with text';
72}