Skip to content

Instantly share code, notes, and snippets.

@alixaxel
Created September 7, 2011 23:26
Show Gist options
  • Save alixaxel/1202123 to your computer and use it in GitHub Desktop.
Save alixaxel/1202123 to your computer and use it in GitHub Desktop.
<?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
?>
@xeoncross
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment