yf_utf8_clean classDo clean UTF8-encoded text from unwanted chars.
| File | plugins/common/classes/common/yf_utf8_clean.class.php |
|---|---|
| Use | _class('utf8_clean', 'classes/common/') |
A site sets them in its config with $PROJECT_CONF['utf8_clean']['NAME'] = ...;
| Property | Default | Description |
|---|---|---|
$UTF8_LOWER_ACCENTS@conf_skip |
|
|
$UTF8_UPPER_ACCENTS@conf_skip |
|
|
$fss_exists |
null |
None without a leading underscore.
_ hooks and helpers; not routable as page actionspublic function _do($text = '', $params = [])
Do clean.
public function _do($text = '', $params = [])
{
if ( ! strlen($text)) {
return '';
}
$text = utf8_strip_ascii_ctrl($text);
if ((SEARCH_COUNTRY == 'ua' || $params['country'] == 'ua') && preg_match('/[а-яА-Я]/', $text)) {
$text = $this->_utf8_bad_for_rus_clean($text);
} else {
$text = $this->_utf8_strip_non_ascii($text);
}
if ($params['simple_chars']) {
$allowed_symbols = implode('', array_keys($this->UTF8_LOWER_ACCENTS)) . implode('', array_keys($this->UTF8_UPPER_ACCENTS));
if ((SEARCH_COUNTRY == 'ua' || $params['country'] == 'ua')) {
$allowed_symbols .= 'а-яА-Я';
}
$text = preg_replace('/[^a-z0-9' . $allowed_symbols . ' ]/', '', $text);
}
return $text;
}
public function _init()
Constructor.
public function _init()
{
// TODO: check and enable: require_php_lib('yf_utf8_funcs') and set $this->fss_exists = function_exists('fss_prep_replace')
return false;
}
public function _unaccent($str = '')
Unaccent.
public function _unaccent($str = '')
{
if ( ! common()->is_utf8($str)) {
$str = mb_convert_encoding($str, 'UTF-8', 'ISO-8859-1');
}
if ($this->fss_exists && ! ($GLOBALS['force_strtr'] ?? null)) {
if ( ! isset($GLOBALS['fss']['unaccent'])) {
$s = array_merge($this->UTF8_UPPER_ACCENTS, $this->UTF8_LOWER_ACCENTS);
$GLOBALS['fss']['unaccent'] = fss_prep_replace($s);
}
$str = fss_exec_replace(($GLOBALS['fss']['unaccent'] ?? null), $str);
return $str;
}
$str = strtr($str, $this->UTF8_UPPER_ACCENTS);
$str = strtr($str, $this->UTF8_LOWER_ACCENTS);
return $str;
}
public function _unaccent_test2($string = '')
public function _unaccent_test2($string = '')
{
$transliteration = [
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A',
'Å' => 'A', 'Æ' => 'A', 'Ā' => 'A', 'Ą' => 'A', 'Ă' => 'A',
'Ç' => 'C', 'Ć' => 'C', 'Č' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C',
'Ď' => 'D', 'Đ' => 'D', 'È' => 'E', 'É' => 'E', 'Ê' => 'E',
'Ë' => 'E', 'Ē' => 'E', 'Ę' => 'E', 'Ě' => 'E', 'Ĕ' => 'E',
'Ė' => 'E', 'Ĝ' => 'G', 'Ğ' => 'G', 'Ġ' => 'G', 'Ģ' => 'G',
'Ĥ' => 'H', 'Ħ' => 'H', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
'Ï' => 'I', 'Ī' => 'I', 'Ĩ' => 'I', 'Ĭ' => 'I', 'Į' => 'I',
'İ' => 'I', 'IJ' => 'IJ', 'Ĵ' => 'J', 'Ķ' => 'K', 'Ľ' => 'K',
'Ĺ' => 'K', 'Ļ' => 'K', 'Ŀ' => 'K', 'Ł' => 'L', 'Ñ' => 'N',
'Ń' => 'N', 'Ň' => 'N', 'Ņ' => 'N', 'Ŋ' => 'N', 'Ò' => 'O',
'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'Oe', 'Ø' => 'O',
'Ō' => 'O', 'Ő' => 'O', 'Ŏ' => 'O', 'Œ' => 'OE', 'Ŕ' => 'R',
'Ř' => 'R', 'Ŗ' => 'R', 'Ś' => 'S', 'Ş' => 'S', 'Ŝ' => 'S',
'Ș' => 'S', 'Š' => 'S', 'Ť' => 'T', 'Ţ' => 'T', 'Ŧ' => 'T',
'Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'Ue',
'Ū' => 'U', 'Ů' => 'U', 'Ű' => 'U', 'Ŭ' => 'U', 'Ũ' => 'U',
'Ų' => 'U', 'Ŵ' => 'W', 'Ŷ' => 'Y', 'Ÿ' => 'Y', 'Ý' => 'Y',
'Ź' => 'Z', 'Ż' => 'Z', 'Ž' => 'Z', 'à' => 'a', 'á' => 'a',
'â' => 'a', 'ã' => 'a', 'ä' => 'ae', 'ā' => 'a', 'ą' => 'a',
'ă' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'ć' => 'c',
'č' => 'c', 'ĉ' => 'c', 'ċ' => 'c', 'ď' => 'd', 'đ' => 'd',
'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ē' => 'e',
'ę' => 'e', 'ě' => 'e', 'ĕ' => 'e', 'ė' => 'e', 'ƒ' => 'f',
'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'ĥ' => 'h',
'ħ' => 'h', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
'ī' => 'i', 'ĩ' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ı' => 'i',
'ij' => 'ij', 'ĵ' => 'j', 'ķ' => 'k', 'ĸ' => 'k', 'ł' => 'l',
'ľ' => 'l', 'ĺ' => 'l', 'ļ' => 'l', 'ŀ' => 'l', 'ñ' => 'n',
'ń' => 'n', 'ň' => 'n', 'ņ' => 'n', 'ʼn' => 'n', 'ŋ' => 'n',
'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'oe',
'ø' => 'o', 'ō' => 'o', 'ő' => 'o', 'ŏ' => 'o', 'œ' => 'oe',
'ŕ' => 'r', 'ř' => 'r', 'ŗ' => 'r', 'ś' => 's', 'š' => 's',
'ť' => 't', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'ue',
'ū' => 'u', 'ů' => 'u', 'ű' => 'u', 'ŭ' => 'u', 'ũ' => 'u',
'ų' => 'u', 'ŵ' => 'w', 'ÿ' => 'y', 'ý' => 'y', 'ŷ' => 'y',
'ż' => 'z', 'ź' => 'z', 'ž' => 'z', 'ß' => 'ss', 'ſ' => 'ss',
];
$string = strtr($string, $transliteration);
return $string;
}
public function _unaccent_test3($text = '')
public function _unaccent_test3($text = '')
{
static $search;
if ( ! $search) {
$search = [];
// Get the HTML entities table into an array
$trans = [
'"' => '"',
'&' => '&',
'\'' => ''',
'<' => '<',
'>' => '>',
];
// Go through the entity mappings one-by-one
foreach ((array) $trans as $literal => $entity) {
// Make sure we don't process any other characters
// such as fractions, quotes etc:
if (ord($literal) >= 192) {
// Get the accented form of the letter
// Get e.g. 'E' from the string 'É'
$search[mb_convert_encoding($literal, 'UTF-8', 'ISO-8859-1')] = $entity[1];
}
}
}
return strtr($text, $search);
}
public function _utf8_bad_for_rus_clean($str)
Special for the russian language.
public function _utf8_bad_for_rus_clean($str)
{
$UTF8_BAD =
'([\x00-\x7F]' . // ASCII (including control chars)
'|[\xC2-\xDF][\x80-\xBF]' . // non-overlong 2-byte
'|\xE0[\xA0-\xBF][\x80-\xBF]' . // excluding overlongs
'|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' . // straight 3-byte
'|\xED[\x80-\x9F][\x80-\xBF]' . // excluding surrogates
'|\xF0[\x90-\xBF][\x80-\xBF]{2}' . // planes 1-3
'|[\xF1-\xF3][\x80-\xBF]{3}' . // planes 4-15
'|\xF4[\x80-\x8F][\x80-\xBF]{2}' . // plane 16
'|(.{1}))'; // invalid byte
ob_start();
while (preg_match('/' . $UTF8_BAD . '/S', $str, $matches)) {
if ( ! isset($matches[2])) {
echo $matches[0];
}
$str = substr($str, strlen($matches[0]));
}
$result = ob_get_clean();
return $result;
}
public function _utf8_strip_non_ascii($str)
Strip out all non-7bit ASCII bytes. If you need to transmit a string to system which you know can only support 7bit ASCII, you could use this function.
Returns string with non ASCII bytes removed
public function _utf8_strip_non_ascii($str)
{
ob_start();
while (preg_match('/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S', $str, $matches)) {
if ( ! isset($matches[2])) {
echo $matches[0];
} else {
// Leave accented symbols untouched
if (isset($this->UTF8_LOWER_ACCENTS[$matches[0]]) || isset($this->UTF8_UPPER_ACCENTS[$matches[0]])) {
echo $matches[0];
}
}
$str = substr($str, strlen($matches[0]));
}
$result = ob_get_clean();
return $result;
}