diff options
author | Urban Müller | 2012-05-15 13:39:44 +0000 |
---|---|---|
committer | Urban Müller | 2012-05-15 13:39:44 +0000 |
commit | 14f9b743304695c729f4e5e7a5a36fbd4547de41 (patch) | |
tree | 573b0e75d098dc74ff40c882cb82378b69ed2971 | |
parent | e2974f041d43b0ce99488027dd38187d606dd552 (diff) | |
download | itools-14f9b743304695c729f4e5e7a5a36fbd4547de41.tar.gz itools-14f9b743304695c729f4e5e7a5a36fbd4547de41.tar.bz2 itools-14f9b743304695c729f4e5e7a5a36fbd4547de41.zip |
support arrays in any2utf8
-rw-r--r-- | it.class | 24 | ||||
-rwxr-xr-x | tests/it.t | 4 |
2 files changed, 19 insertions, 9 deletions
@@ -410,19 +410,27 @@ static function grep($pattern, $array, $p = array()) /** * Convert string to utf8 if it was not already utf-8 before. Also handles double encoding - * @param $value String to convert + * @param $value String or array to convert * @param $errmsg Error message to output if anything needed to be done * @return Same string in utf-8 encoding */ function any2utf8($value, $errmsg = "") { - if (grapheme_strlen($value) === null) - list($value, $error) = array(utf8_encode($value), utf8_encode("$errmsg: incorrect utf8-encoding. input=" . trim($value))); - if (preg_match('/\xc3\x83\xc2([\xbc\xa9\xa4\xb6\xa8\xa2\xa0\xb4\xaa\xa7\x84\xab\xae\x9c\xaf\x96\xb2\xbb\xb9\x9f])/', $value)) - list($value, $error) = array(preg_replace('/\xc3\x83\xc2([\xbc\xa9\xa4\xb6\xa8\xa2\xa0\xb4\xaa\xa7\x84\xab\xae\x9c\xaf\x96\xb2\xbb\xb9\x9f])/', "\xc3\$1", $value), utf8_encode("$errmsg: double utf8-encoding. input=" . trim($value))); - - if ($error && $errmsg) - it::error(array('title' => $error, 'skipfiles' => "it_html")); + if (is_array($value)) + { + foreach ($value as $idx => $v) + if (is_string($v) || is_array($v)) + $value[$idx] = self::any2utf8($v, $errmsg); + } + else + { + if (grapheme_strlen($value) === null) + list($value, $error) = array(utf8_encode($value), utf8_encode("$errmsg: incorrect utf8-encoding. input=" . trim($value))); + if (preg_match('/\xc3\x83\xc2([\xbc\xa9\xa4\xb6\xa8\xa2\xa0\xb4\xaa\xa7\x84\xab\xae\x9c\xaf\x96\xb2\xbb\xb9\x9f])/', $value)) + list($value, $error) = array(preg_replace('/\xc3\x83\xc2([\xbc\xa9\xa4\xb6\xa8\xa2\xa0\xb4\xaa\xa7\x84\xab\xae\x9c\xaf\x96\xb2\xbb\xb9\x9f])/', "\xc3\$1", $value), utf8_encode("$errmsg: double utf8-encoding. input=" . trim($value))); + if ($error && $errmsg) + it::error(array('title' => $error, 'skipfiles' => "it_html")); + } return $value; } @@ -379,4 +379,6 @@ is(it::any2utf8(utf8_encode("Müller")), "Müller", "it::any2utf8 double encoded is(it::any2utf8(utf8_decode("Müller")), "Müller", "it::any2utf8 incorrectly encoded latin1"); is(it::any2utf8("a💚b"), "a💚b", "it::any2utf8 correctly handles 4-byte utf-8 character GREEN HEART"); -?> +is(it::any2utf8(array("foo", utf8_decode("bär"))), array("foo", "bär"), "any2utf8 on arrays"); +is(it::any2utf8(array("foo", array(utf8_decode("bär")))), array("foo", array("bär")), "any2utf8 on recursive arrays"); +is(it::any2utf8(array(1, true, false, null)), array(1, true, false, null), "any2utf8 should leave types alone"); |