summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--it_text.class30
-rwxr-xr-xtests/it_text.t18
2 files changed, 33 insertions, 15 deletions
diff --git a/it_text.class b/it_text.class
index b986e97..3264169 100644
--- a/it_text.class
+++ b/it_text.class
@@ -218,30 +218,30 @@ function set($label, $text = null, $language = null)
*/
function transmogrify($text, $values = null)
{
- while (preg_match('/{([\w.]+)}/', $text, $regs))
+ foreach (preg_split('/{([\w.]+)}/', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $idx => $part)
{
- $path = explode('.', $regs[1]);
- if ($values)
- $value =& $values;
+ if ($idx % 2 == 0) # even offsets are between delimiters
+ $result .= $part;
else
- $value =& $GLOBALS;
-
- # Recurse into nested arrays/object members
- foreach ($path as $key)
{
- if (is_object($value))
- $value =& $value->$key;
- else
- $value =& $value[$key];
+ $value = $values ? $values : $GLOBALS;
+ foreach (explode(".", $part) as $key)
+ {
+ if (is_object($value))
+ $value =& $value->$key;
+ else
+ $value =& $value[$key];
+ }
+
+ $result .= $value;
}
-
- $text = str_replace($regs[0], $value, $text);
}
- return $text;
+ return $result;
}
+
/**
* Re-create php text file from $this->statictext
* @return true if successful, false if not (usually if file is not writeable by user www)
diff --git a/tests/it_text.t b/tests/it_text.t
new file mode 100755
index 0000000..bc4d5c0
--- /dev/null
+++ b/tests/it_text.t
@@ -0,0 +1,18 @@
+#!/www/server/bin/php
+<?php
+
+$obj->x = "attr";
+$obj->y->z = "attr";
+
+is(it_text::transmogrify(""), "");
+is(it_text::transmogrify("foo"), "foo");
+is(it_text::transmogrify("{foo}"), "");
+is(it_text::transmogrify("{foo}", array('foo' => 1)), "1");
+is(it_text::transmogrify("{foo}{foo}", array('foo' => 1)), "11");
+is(it_text::transmogrify("{foo}X", array('foo' => 1)), "1X");
+is(it_text::transmogrify("{foo}X{foo}", array('foo' => 1)), "1X1");
+is(it_text::transmogrify("X{foo}", array('foo' => 1)), "X1");
+is(it_text::transmogrify("X{foo}{bar}", array('foo' => 1, 'bar' => 2)), "X12");
+is(it_text::transmogrify("X{foo}", array('foo' => "&")), "X&");
+is(it_text::transmogrify("X{x}", $obj), "Xattr");
+is(it_text::transmogrify("X{y.z}", $obj), "Xattr");