Created
September 7, 2011 23:26
-
-
Save alixaxel/1202123 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function Filter($data, $control = true, $encoding = null) | |
{ | |
if (is_array($data) === true) | |
{ | |
$result = array(); | |
foreach ($data as $key => $value) | |
{ | |
$result[self::Filter($key, $control, $encoding)] = self::Filter($value, $control, $encoding); | |
} | |
return $result; | |
} | |
else if (is_string($data) === true) | |
{ | |
if (is_null($encoding) === true) | |
{ | |
$encoding = 'UTF-8'; | |
if (extension_loaded('mbstring') === true) | |
{ | |
$encoding = mb_detect_encoding($data, 'auto'); | |
} | |
} | |
$data = @iconv($encoding, 'UTF-8//IGNORE', $data); | |
if ($control === true) | |
{ | |
return preg_replace('~\p{C}+~u', '', $data); | |
} | |
return preg_replace(array('~\r[\n]?~', '~[^\P{C}\t\n]+~u'), array("\n", ''), $data); | |
} | |
return $data; | |
} | |
// http://stackoverflow.com/questions/910793/php-detect-encoding-and-make-everything-utf-8 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For those of you wondering what the ending regex is all about, see the manual on unicode control characters added to PREG in PHP 5.1.