function testBrr() { $str = 'a+(b+(c+1))'; $brr = new Brr(); $substituted = $brr->sub($str); $this->assertEqual($substituted, 'a+([0]b+([1]c+1)[1])[0]'); $substituted = str_replace('a', 'A', $substituted); $substituted = str_replace('b', 'B', $substituted); $substituted = str_replace('c', 'C', $substituted); $substituted = $brr->subback($substituted); $this->assertEqual($substituted, 'A+(B+(C+1))'); }
/** * Parse a string into an expression */ function parse($str) { // replace backslashes with sqrt, eg \2 -> sqrt(2) $ss = preg_replace(':\\\\([\\w\\.]+):', 'sqrt($1)', $str); // this replaces \(stuff) with sqrt(stuff), eg \(a^2+b^2) -> sqrt(a^2+b^2) $ss = preg_replace(':\\\\\\((.+)\\):', 'sqrt($1)', $ss); // equations/inequalities containing unary '-' are parsed wrongly // fix it by putting both part in brackets $ss = preg_replace(':(.+?)(<=|>=|<|>|=)(.+):', '($1)$2($3)', $ss); //echo $ss.'<br>'; $brr = new Brr(); $c = $brr->sub($ss); //echo "parse $c\n"; $this->parse1($c, 0); array_splice($this->brackets, 0); array_splice($this->brackets_re, 0); }