From b8eb9732f5567a87a6325aaa3075901846b79409 Mon Sep 17 00:00:00 2001 From: Shakir El Amrani Date: Sun, 27 Nov 2022 18:23:28 +0100 Subject: [PATCH 1/7] change: formating enhacements --- mathexphtml.php | 165 ++++++++++++++++++++++-------------------------- 1 file changed, 77 insertions(+), 88 deletions(-) diff --git a/mathexphtml.php b/mathexphtml.php index 1aa3448..6771cbf 100644 --- a/mathexphtml.php +++ b/mathexphtml.php @@ -1,18 +1,18 @@ ', ',' )); + $this->tokens = $this->str2tokens($str); } + function operators() { return _OPERATORS; } + function str2tokens($str) { $tokens = preg_split('/([<>()*\/=^+-])\s*|([\d.]+)\s*|(\w+)\s*|({\w+})\s*/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); + return $tokens; } + function tokentype($token) { return (is_numeric($token) ? "value" : (in_array($token, _OPERATORS) ? "operator" : (preg_match('/^[\w-]+$/', $token) ? "alphanum" : @@ -47,86 +52,80 @@ function tokentype($token) function mathhtml($a, $o, $b) { $html = ""; + switch ($o) { case '+': case '-': - case '=': case '<': case '>': case ',': $html = "$a $o $b"; - break; + break; + case '*': - $html = "$a • $b"; - break; + $html = "$a • $b"; + break; + case '^': $html = "$a$b"; - break; + break; + case '/': $html = "
"; $html .= "$a"; // numerator $html .= "/"; $html .= "$b"; // denominator $html .= "
"; - break; + break; } + return ($html); } - function testknownoperator($token){ + + function testknownoperator($token) + { $pattern = "/([(),+])\s*/"; - //$str = strip_tags($token); $test = preg_split($pattern, $token, 8, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); - - //echo "
"; - //print_r($test); - //echo "
"; - $html = ""; - if (strip_tags($test[0]) == 'sum'){ $html = "". "".$test[4]."". "". "".$test[2]."". ""; - }/* - else if (strip_tags($test[0]) == 'int'){ - $html = "". - "".$test[4]."". - "". - "".$test[2]."". - ""; } - */ + return $html; } - function formatelement($tokens) + + function formatelement($tokens) { $len = count($tokens); - for ($i = 0;$i < $len;$i++) - { + for ($i = 0;$i < $len;$i++) { $a = $tokens[$i]; $b = ($i < $len - 1) ? trim($tokens[$i + 1]) : " "; - if ($this->tokentype($a) == "alphanum") - { - if ($b[0] == '('){ + + if ($this->tokentype($a) == "alphanum") { + if ($b[0] == '(') { $a = "$a"; + } else { + $a = "$a"; } - else $a = "$a"; - } - else if ($this->tokentype($a) == "value"){ + } elseif ($this->tokentype($a) == "value") { $a = "$a"; - } else if ($this->tokentype($a) == "vector"){ + } elseif ($this->tokentype($a) == "vector") { $a = str_replace(array("{", "}"), "", $a); $s = strlen($a); $a = " $a". "". ""; } + $tokens[$i] = $a; } + return $tokens; } @@ -136,143 +135,133 @@ function parsepar($tokens) $pos = 0; $len = count($tokens); $level = 0; - while ($pos < $len) - { + while ($pos < $len) { $a = $tokens[$pos]; - while ($a != "(" && $pos < $len) - { + + while ($a != "(" && $pos < $len) { $pos++; $a = $tokens[$pos]; } - while ($a != ")" && $pos < $len) - { - if ($a == "(") - { + + while ($a != ")" && $pos < $len) { + if ($a == "(") { $start = $pos; $level++; } + $pos++; $a = $tokens[$pos]; } - if ($a == ")") - { + + if ($a == ")") { $end = $pos; $level--; $subtokens = array_slice($tokens, $start + 1, $end - $start - 1); array_splice($tokens, $start + 1, $end - $start); $tokens[$start] = "(" . $this->exphtml($subtokens) . ")"; - //echo "sub ".$subtokens[0]."=>".count($subtokens)."
"; $pos = $start; } + $pos++; } - /* - print_r($tokens); - echo "
"; - print_r($subtokens); - echo "
"; - echo "level ".$level."
"; - */ - if ($level != 0) $tokens = $this->parsepar($tokens); + + if ($level != 0) { + $tokens = $this->parsepar($tokens); + } + return $tokens; } + function exphtml($tokens) { $len = count($tokens); - if ($len <= 2) - { + if ($len <= 2) { return $tokens[0]; } - // merge function - for ($i = 0;$i < $len;$i++) - { + + for ($i = 0;$i < $len;$i++) { $a = $tokens[$i]; $b = ($i < $len - 1) ? trim($tokens[$i + 1]) : " "; - if ($this->tokentype(strip_tags($a)) == "alphanum") - { - if ($b[0] == '('){ + if ($this->tokentype(strip_tags($a)) == "alphanum") { + if ($b[0] == '(') { $tokens[$i] = $a . $b; array_splice($tokens, $i + 1, 1); $html = $this->testknownoperator($tokens[$i]); - if ($html != "") $tokens[$i] = $html; + if ($html != "") { + $tokens[$i] = $html; + } } } } + $len = count($tokens); - //echo "length ".$len."
"; $pos = 0; - while ($pos < $len - 2) - { + while ($pos < $len - 2) { $a = $tokens[$pos]; $o = $tokens[$pos + 1]; $b = $tokens[$pos + 2]; - if ($this->tokentype($o) == "operator" && $o == '^') - { + if ($this->tokentype($o) == "operator" && $o == '^') { $a = $this->mathhtml($a, $o, $b); $tokens[$pos] = $a; array_splice($tokens, $pos + 1, 2); $len = count($tokens); + } else { + $pos += 2; } - else $pos += 2; } $pos = 0; - while ($pos < $len - 2) - { + while ($pos < $len - 2) { $a = $tokens[$pos]; $o = $tokens[$pos + 1]; $b = $tokens[$pos + 2]; - if ($this->tokentype($o) == "operator" && ($o == "/" || $o == "*")) - { + if ($this->tokentype($o) == "operator" && ($o == "/" || $o == "*")) { $a = $this->mathhtml($a, $o, $b); $tokens[$pos] = $a; array_splice($tokens, $pos + 1, 2); $len = count($tokens); } - else $pos += 2; + else { + $pos += 2; + } } $pos = 0; $a = $tokens[$pos]; - while ($pos < $len - 2) - { + while ($pos < $len - 2) { $o = $tokens[$pos + 1]; $b = $tokens[$pos + 2]; - if ($this->tokentype($o) == "operator") - { + if ($this->tokentype($o) == "operator") { $a = $this->mathhtml($a, $o, $b); } + $pos += 2; } + if ($pos < $len-1 && $tokens[$pos+1][0] == '(') { $a .= $tokens[$pos+1]; } return $a; } + function printtokens() { echo "
";
-        foreach ($this->tokens as $token)
-        {
+        foreach ($this->tokens as $token) {
             $token = trim($token);
             echo $token . "  is " . $this->tokentype($token) . "
"; } - echo "
"; - echo "
"; + echo "
"; } + function gethtml() { $tokens = $this->parsepar($this->tokens); - //echo "
"; - //print_r($tokens); - //echo "
"; echo "" . $this->exphtml($tokens) . ""; } } - -?> From aa8e0f38e7e5fcaf8e456cceb37b43d487af8120 Mon Sep 17 00:00:00 2001 From: Shakir El Amrani Date: Sun, 27 Nov 2022 18:31:26 +0100 Subject: [PATCH 2/7] change: added visibility properties to class methods --- mathexphtml.php | 51 +++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/mathexphtml.php b/mathexphtml.php index 6771cbf..9a929ec 100644 --- a/mathexphtml.php +++ b/mathexphtml.php @@ -13,7 +13,7 @@ class mathExpHtml { private $tokens; - function __construct($str) + public function __construct($str) { define('_OPERATORS', array( '(', @@ -32,24 +32,41 @@ function __construct($str) $this->tokens = $this->str2tokens($str); } - function operators() + public function printtokens() + { + echo "
";
+        foreach ($this->tokens as $token) {
+            $token = trim($token);
+            echo $token . "  is " . $this->tokentype($token) . "
"; + } + echo "
"; + } + + public function gethtml() + { + $tokens = $this->parsepar($this->tokens); + echo "" . $this->exphtml($tokens) . ""; + } + + private function operators() { return _OPERATORS; } - function str2tokens($str) + private function str2tokens($str) { $tokens = preg_split('/([<>()*\/=^+-])\s*|([\d.]+)\s*|(\w+)\s*|({\w+})\s*/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); return $tokens; } - function tokentype($token) + private function tokentype($token) { return (is_numeric($token) ? "value" : (in_array($token, _OPERATORS) ? "operator" : (preg_match('/^[\w-]+$/', $token) ? "alphanum" : (preg_match('/({\w+})/', $token)?"vector":"none")))); } - function mathhtml($a, $o, $b) + + private function mathhtml($a, $o, $b) { $html = ""; @@ -84,7 +101,7 @@ function mathhtml($a, $o, $b) return ($html); } - function testknownoperator($token) + private function testknownoperator($token) { $pattern = "/([(),+])\s*/"; $test = preg_split($pattern, $token, 8, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); @@ -100,7 +117,7 @@ function testknownoperator($token) return $html; } - function formatelement($tokens) + private function formatelement($tokens) { $len = count($tokens); for ($i = 0;$i < $len;$i++) { @@ -129,7 +146,7 @@ function formatelement($tokens) return $tokens; } - function parsepar($tokens) + private function parsepar($tokens) { $tokens = $this->formatelement($tokens); $pos = 0; @@ -172,7 +189,7 @@ function parsepar($tokens) return $tokens; } - function exphtml($tokens) + private function exphtml($tokens) { $len = count($tokens); @@ -248,20 +265,4 @@ function exphtml($tokens) return $a; } - - function printtokens() - { - echo "
";
-        foreach ($this->tokens as $token) {
-            $token = trim($token);
-            echo $token . "  is " . $this->tokentype($token) . "
"; - } - echo "
"; - } - - function gethtml() - { - $tokens = $this->parsepar($this->tokens); - echo "" . $this->exphtml($tokens) . ""; - } } From 4c84743d3a251c639082a77724aadfb626868aa2 Mon Sep 17 00:00:00 2001 From: Shakir El Amrani Date: Sun, 27 Nov 2022 18:32:54 +0100 Subject: [PATCH 3/7] change: uppercase class name --- mathexphtml.php => MathExpHtml.php | 4 ++-- example.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) rename mathexphtml.php => MathExpHtml.php (99%) diff --git a/mathexphtml.php b/MathExpHtml.php similarity index 99% rename from mathexphtml.php rename to MathExpHtml.php index 9a929ec..db51a2b 100644 --- a/mathexphtml.php +++ b/MathExpHtml.php @@ -1,7 +1,7 @@ printtokens(); $parser->gethtml(); From 704587a4a167185033c06e93a4a9888f6f98a162 Mon Sep 17 00:00:00 2001 From: Shakir El Amrani Date: Sun, 27 Nov 2022 17:39:25 +0000 Subject: [PATCH 4/7] change: fixed formatting issues in the example page --- example.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/example.php b/example.php index 2eeeed1..24fc172 100644 --- a/example.php +++ b/example.php @@ -1,7 +1,6 @@ - +