2010-10-15 03:03:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Support class for converting unicode strings into a suitable 7-bit ASCII equivalent.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
2010-10-15 03:04:34 +02:00
|
|
|
* <code>
|
2012-06-15 05:54:47 +02:00
|
|
|
* $tr = new SS_Transliterator();
|
2010-10-15 03:03:43 +02:00
|
|
|
* $ascii = $tr->toASCII($unicode);
|
2010-10-15 03:04:34 +02:00
|
|
|
* </code>
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-15 03:04:34 +02:00
|
|
|
* @subpackage model
|
2010-10-15 03:03:43 +02:00
|
|
|
*/
|
2012-06-15 05:54:47 +02:00
|
|
|
class SS_Transliterator extends Object {
|
2010-10-15 03:03:43 +02:00
|
|
|
/**
|
|
|
|
* Allow the use of iconv() to perform transliteration. Set to false to disable.
|
|
|
|
* Even if this variable is true, iconv() won't be used if it's not installed.
|
|
|
|
*/
|
|
|
|
static $use_iconv = false;
|
2012-04-04 16:59:30 +02:00
|
|
|
|
2010-10-15 03:03:43 +02:00
|
|
|
/**
|
|
|
|
* Convert the given utf8 string to a safe ASCII source
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function toASCII($source) {
|
2010-10-15 03:03:43 +02:00
|
|
|
if(function_exists('iconv') && self::$use_iconv) return $this->useIconv($source);
|
|
|
|
else return $this->useStrTr($source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliteration using strtr() and a lookup table
|
|
|
|
*/
|
|
|
|
protected function useStrTr($source) {
|
2011-10-10 10:14:19 +02:00
|
|
|
$table = array(
|
|
|
|
'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
|
|
|
|
'À'=>'A', 'Á'=>'A', 'Â'=>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2011-10-10 10:14:19 +02:00
|
|
|
);
|
2010-10-15 03:03:43 +02:00
|
|
|
|
2011-10-10 10:14:19 +02:00
|
|
|
return strtr($source, $table);
|
2010-10-15 03:03:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliteration using iconv()
|
|
|
|
*/
|
|
|
|
protected function useIconv($source) {
|
|
|
|
return iconv("utf-8", "us-ascii//IGNORE//TRANSLIT", $source);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|