extranet/library/i18n/cleanchar.php
2011-12-15 11:24:51 +00:00

92 lines
1.8 KiB
PHP

<?php
/**
* @file
* Miscellaneous functions to clean string.
*
*/
/**
* Clean up a string value.
*
* Resulting string contains only alphanumerics and separators.
*
* @param $string
* A string to clean.
* @param $clean_slash
* Whether to clean slashes from the given string.
* @return
* The cleaned string.
*/
function cleanstring($string) {
$transliterate = TRUE;
$reduce_ascii = FALSE;
$output = $string;
// Remove accents and transliterate
if ($transliterate) {
static $i18n_loaded = false;
static $translations = array();
if (!$i18n_loaded) {
$path = realpath(dirname(__FILE__));
if (is_file($path .'/i18n-ascii.txt')) {
$translations = parse_ini_file($path .'/i18n-ascii.txt');
}
$i18n_loaded = true;
}
$output = strtr($output, $translations);
}
// Reduce to the subset of ASCII96 letters and numbers
if ($reduce_ascii) {
$pattern = '/[^a-zA-Z0-9\/]+/ ';
$output = preg_replace($pattern, $separator, $output);
}
return $output;
}
function cleanutf8($string) {
$transliterate = TRUE;
$output = $string;
// Remove accents and transliterate
if ($transliterate) {
static $i18n_loaded = false;
static $translations = array();
if (!$i18n_loaded) {
$path = realpath(dirname(__FILE__));
if (is_file($path .'/i18n-ascii.txt')) {
$translations = parse_ini_file($path .'/i18n-ascii.txt');
}
$i18n_loaded = true;
}
$output = strtr($output, $translations);
}
return $output;
}
// Fixes the encoding to uf8
function fixEncoding($in_str)
{
$cur_encoding = mb_detect_encoding($in_str) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
return $in_str;
else
return utf8_encode($in_str);
} // fixEncoding
function cleanstring_deep($value)
{
$value = is_array($value) ?
array_map('cleanstring_deep', $value) :
cleanstring($value);
return $value;
}
?>