87 lines
2.0 KiB
PHP
87 lines
2.0 KiB
PHP
<?php
|
|
/**
|
|
* Miscellaneous functions to clean string.
|
|
*/
|
|
class Scores_Locale_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.
|
|
*/
|
|
public static 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 .'/String/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;
|
|
}
|
|
|
|
public static 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 .'/String/i18n-ascii.txt');
|
|
}
|
|
$i18n_loaded = true;
|
|
}
|
|
|
|
$output = strtr($output, $translations);
|
|
}
|
|
return $output;
|
|
}
|
|
|
|
// Fixes the encoding to uf8
|
|
public static 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
|
|
|
|
public static function cleanstring_deep($value)
|
|
{
|
|
$value = is_array($value) ?
|
|
array_map('self::cleanstring_deep', $value) :
|
|
cleanstring($value);
|
|
return $value;
|
|
}
|
|
} |