/** * Recursively cleans arrays, objects, and strings. Removes ASCII control * codes and converts to the requested charset while silently discarding * incompatible characters. * * UTF8::clean($_GET); // Clean GET data * * @param mixed $var variable to clean * @param string $charset character set, defaults to Phalcana\Phalcana::$charset * @return mixed * @uses UTF8::clean * @uses UTF8::stripAsciiCtrl * @uses UTF8::isAscii */ public static function clean($var, $charset = null) { if (!$charset) { // Use the application character set $charset = Phalcana::$charset; } if (is_array($var) || is_object($var)) { foreach ($var as $key => $val) { // Recursion! $var[UTF8::clean($key)] = UTF8::clean($val); } } elseif (is_string($var) && $var !== '') { // Remove control characters $var = UTF8::stripAsciiCtrl($var); if (!UTF8::isAscii($var)) { // Temporarily save the mb_substitute_character() value into a variable $mb_substitute_character = mb_substitute_character(); // Disable substituting illegal characters with the default '?' character mb_substitute_character('none'); // convert encoding, this is expensive, used when $var is not ASCII $var = mb_convert_encoding($var, $charset, $charset); // Reset mb_substitute_character() value back to the original setting mb_substitute_character($mb_substitute_character); } } return $var; }
/** * Tests UTF8::stripAsciiCtrl * * @test * @dataProvider provider_stripAsciiCtrl */ public function test_stripAsciiCtrl($input, $expected) { $this->assertSame($expected, UTF8::stripAsciiCtrl($input)); }