diff --git a/src/PowerReplace.php b/src/PowerReplace.php index e5e669d..2a278de 100644 --- a/src/PowerReplace.php +++ b/src/PowerReplace.php @@ -84,7 +84,8 @@ protected function callbackReplace($key) $functionsHolder = $this->functionsHolder; if ($functionsHolder) { - $functions = $this->explode($key, $this->markerFunctionsSeparator); + //$functions = $this->explode($key, $this->markerFunctionsSeparator); + $functions = $this->explodeWithFunctions($key, $this->markerFunctionsSeparator); $key = array_shift($functions); } else { $functions = false; @@ -199,6 +200,45 @@ protected function explode($string, $separator = ',') return $result; } + protected function explodeWithFunctions($string, $separator = ':') + { + if (!strpos($string, $separator)) { + return [$string]; + } + + $result = []; + $accumulatedString = ''; + $value = 0; + $openedBracket = 0; + $openedQuote = ''; + $stringArray = str_split($string); + foreach ($stringArray as $char) { + if (!$openedBracket and $char == $separator) { + if ($accumulatedString) { + $result[] = $accumulatedString; + $accumulatedString = ''; + } + continue; + } + + if ($char == '(') { + $openedBracket++; + } + + if ($char == ')') { + $openedBracket--; + } + + $accumulatedString .= $char; + } + + if ($accumulatedString) { + $result[] = $accumulatedString; + } + + return $result; + } + /** * Set whole data array. * diff --git a/test/StringReplaceTest.php b/test/StringReplaceTest.php index d893155..ffbe891 100644 --- a/test/StringReplaceTest.php +++ b/test/StringReplaceTest.php @@ -179,6 +179,13 @@ public function testPower() $instance->apple_count = 2; $this->assertEquals('Vegetables I have: apples 2, oranges 1', $instance->replace($string)); + // + $string = 'Vegetables I have: #apple_count:prefix("apples: "):addcomma(1)##orange_count:prefix("oranges: "):addcomma(1)#'; + $instance = new PowerReplace(); + $instance->orange_count = 1; + $instance->apple_count = 2; + $this->assertEquals('Vegetables I have: apples: 2, oranges: 1', $instance->replace($string)); + } public function testPowerWrongFunctions()